python - psycopg2 copy_to 在插入期间将浮点值解释为字符串
问题描述
我有一个熊猫数据框,在打印信息时看起来像这样print(df.info())
copy_to
现在,我正在尝试使用 psycopg2 的函数将其插入到 postgres 中的表中。它看起来像这样
def copy(conn, table, df):
sio = StringIO()
# Handle NaN
# Since to_csv writes NaN as empty string, it causes trouble with copy_to since NaN is present in Numeric types
# So converting NaN in Numeric types to -1
df = set_empty_defaults_df(df)
sio.write(df.to_csv(index=None, header=None, sep="|"))
sio.seek(0)
print("Table %s" % (table))
with conn.cursor() as c:
c.copy_from(
file=sio,
table=table,
sep="|"
)
conn.commit()
当我运行这个时,我得到错误`DataError: invalid input syntax for integer: "0.0" CONTEXT: COPY time, line 1, column hour: "0.0"
很奇怪 psycopg2 将双 0.0 解释为字符串而不是十进制值,因为 pandas 具有正确的数据类型。
我在这里想念什么?
解决方案
我相信“作为字符串”位是一个红鲱鱼 - 那些引号可能只是为了分隔值。你真正的问题是你试图在一个整数列中存储一个带小数的值。
例如,在psql
提示符下尝试以下操作。您将收到相同的错误消息:
db=> create temporary table t (x integer) ;
CREATE TABLE
db=> \copy t(x) from stdin
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 1.0
>> \.
ERROR: invalid input syntax for type integer: "1.0"
CONTEXT: COPY t, line 1, column x: "1.0"
所以你需要让 pandas 打印那些不带小数的列,也许通过将它们转换为整数,或者使用一些花哨的格式化选项to_csv
(我不记得有什么)。
推荐阅读
- css - 使用 nth-last-child(odd) 和 (even) 将 css 添加到图像
- sql - SSIS:Excel源检测类型
- c# - WPF - 关于使用树视图样式菜单结构的建议
- apache-flink - 基于 Date 类型的字段过滤 Flink Table
- python - 熊猫在许多列上
- android - 如何检查自定义 DialogFragment 是否显示?
- laravel - Laravel/Eloquent 通过一个约会成员表从一个多态的“member_id”获取所有约会
- python - 如何从链式查询集中删除重复项?
- c - 在 C LINUX 中从串行端口读取不一致
- powershell - Powershell Trasnscript - 输出到文件而不显示在控制台中