postgresql - 使用 PostgreSQL plpython3u 函数返回一个表
问题描述
我要回表。该函数获取一个数组(查询是'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 语法?
解决方案
以下是我尝试获取包含四列的一行表格作为输出的步骤。最后一步有解决方案,第一步是重现错误的另一种方法。
检查 np.array
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)
您需要重塑数组,使其具有您想要获得的维度。在这种情况下,它是暗淡的(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)
推荐阅读
- mysql - Mysql 8 表大小
- java - 如何使用 java 在 amazon s3 中存储文件
- python - 将 one-hot 编码维度转换为位置 1 的索引
- db2 - 如何从 Datastage 中的输入字符串中删除垃圾字符
- hadoop - 有哪些开源解决方案可以使用 Kafka Connect 将数据从 Kafka 移动到 HDFS3?
- reactjs - 排序后反应不重新渲染
- cookies - com.au 子域的 Cookie 没有被发回
- rust - 将动态整数映射到 const 整数
- css - 我找不到带有导出数据的 CSS 输入代码库
- javascript - 我不想保存的日期选择器返回时间