- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想退 table 。该函数获取一个数组(查询是'select function_name(array_agg(column_name)) from table_name')
我在下面编码:
create type pddesctype as(
count float,
mean float,
std float,
min float
);
create function pddesc(x numeric[])
returns pddesctype
as $$
import pandas as pd
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
return count, mean, std, min
$$ language plpython3u;
此代码只在一列上生成数组。(漂浮、漂浮、漂浮……)
我试过了
create function pddesc(x numeric[])
returns table(count float, mean float, std float, min float)
as $$
import pandas as pd
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
return count, mean, std, min
$$ language plpython3u;
但是有一个错误:
ERROR: key "count" not found in mapping
HINT: To return null in a column, add the value None to the mapping with the key named after the column.
CONTEXT: while creating return value.
我想在不预先创建类型的情况下以列(如表格)显示结果。
如何更改 RETURN/RETURNS 语法?
最佳答案
以下是我尝试获取单行四列表格作为输出的步骤。最后一步有解决方案,第一步是重现错误的另一种方法。
create or replace function pddesc(x numeric[])
returns table(count float, mean float, std float, min float)
as $$
import pandas as pd
import numpy as np
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
## print an INFO of the output:
plpy.info(np.array([count, mean, std, min]))
return np.array([count, mean, std, min])
$$ language plpython3u;
测试失败(重现题目错误):
postgres=# SELECT * FROM pddesc(ARRAY[1,2,3]);
INFO: [3 3 Decimal('1') 1]
ERROR: key "count" not found in mapping
HINT: To return null in a column, add the value None to the mapping with the key named after the column.
CONTEXT: while creating return value
PL/Python function "pddesc"
np.array([...]).reshape(1,-1)
您需要 reshape 数组,使其具有您想要的维度。在这种情况下,它是暗淡的(1 行 x 4 列),.reshape(1,-1)
表示 1 行和 -1(= 任何需要的)列
create or replace function pddesc(x numeric[])
returns table(count float, mean float, std float, min float)
as $$
import pandas as pd
import numpy as np
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
## print an INFO of the output:
plpy.info(np.array([count, mean, std, min]).reshape(1,-1))
return np.array([count, mean, std, min]).reshape(1,-1)
## or with the same result:
# return np.hstack((count, mean, std, min)).reshape(1,-1)
$$ language plpython3u;
测试:
postgres=# SELECT * FROM pddesc(ARRAY[1,2,3]);
INFO: [[3 3 Decimal('1') 1]]
count | mean | std | min
-------+------+-----+-----
3 | 3 | 1 | 1
(1 row)
关于postgresql - 使用 PostgreSQL plpython3u 函数返回一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658930/
我有一个 plypython 函数,它执行一些 json 魔术。为此,它显然导入了 json 库。 是否在每次调用函数时调用导入?是否有任何我必须注意的性能影响? 最佳答案 import 在每次函数调
在 Postgresql 版本 10 中创建扩展时出现错误 could not load library "C:/Program Files/PostgreSQL/10/lib/plpython3.d
我正在使用 PostgreSQL 9.4.7 和 Python 2.7.6。我正在编写一个 plpython 函数来更新用户表中的一行,我的代码如下 - CREATE FUNCTION update_
CREATE OR REPLACE FUNCTION CLEAN_STRING(in_str varchar) returns varchar AS $$ def strip_slashes(in_s
plpython、plpythonu、plpython2u 之间有什么区别。 u 和 2u 是什么意思。 环顾四周,我找不到任何关于此事的信息。 最佳答案 PL/Python 过程语言允许用 Pyth
我有 postgres 图像:https://hub.docker.com/_/postgres 我正在尝试使用以下命令创建 python 扩展: create extension plpythonu
我正在使用(或尝试)以下功能: CREATE FUNCTION replace_value(data JSON, key text, value anyelement) RETURNS JSON AS
我安装了以下包: sudo apt-get install postgresql-plpython-8.4 但是在使用的时候: user=# create language plpythonu; 抛出
我正在使用 PostgreSQL 和 plpython 函数。 我有一个函数func1: CREATE OR REPLACE FUNCTION func1(a integer, b timestamp
我正在尝试使用 python 触发器程序实现类似于复制的功能。 程序 CREATE OR REPLACE FUNCTION foo.send_payload() RETURNS trigger AS
我是 python 的新手,我正在尝试在 pl/python 中进行一些服务器编程(以实现更快的处理速度),经过多次尝试后,我决定在此列表中寻找资源以寻求帮助。 我想通过传递 vessel_speed
我有一个名为 tt 的复合类型,供我所有的 plpgsql 和 plpythonu 使用程序。有什么东西吗?访问目录的方式或模式以一致的方式派生类型或可迭代结构以返回无需在 plpythonu 过程中
我想在 pl/python3/postgis 中创建一个返回一组几何记录(例如点)的函数。我设法用我的点的坐标创建了一个字典,但我无法弄清楚如何将它们变成实际的 postgis“点”geom。到目前为
是否可以在 greenplum 数据库中使用 pandas,如果可以,如何使用?我在 greenplum 里面。我正在创建一个函数: CREATE OR REPLACE FUNCTION myfunc
我创建了自己的 python 模块并用 distutils 打包了它。现在我将它安装在一个新系统上(python setup.py install),我试图从 plpython3u 函数调用它,但我收
我正在尝试在我的 Postgres 9.1 中安装 plpython,但它使服务器崩溃: postgres@dataserver1:~> /opt/postgres/9.1/bin/psql -d m
我刚刚在 Mac OS X 10.5(使用端口)上安装了 PostgreSQL 8.3.4,但我不知道如何启用 PL/Python。当我运行 CREATE LANGUAGE plpythonu 时,出
我想在 postgresql 上安装 plpython 来触发 python 当我恢复我的数据库时(我的数据库有一些 python 触发器)出现这个错误(错误消息太大所以我粘贴它的第一行): C:/P
我在循环中遇到问题,我必须比较不同表之间的列。 我有两个表 year2004 和 year2005。两者都包含月份编号和该月份的金额。我想比较两个表中的金额并生成第三个表 year,其中包含月数和该月
我已经使用来自 EnterpriseDB 的安装程序在 Mac OSX 10.6 上安装了 PostgreSQL 9.0.4。并注意到在 plpython 中实现的存储过程使用 python 2.5。
我是一名优秀的程序员,十分优秀!