【问题标题】:pandas to_sql can't adapt type 'dict'pandas to_sql 无法适应类型“dict”
【发布时间】:2021-07-03 20:26:53
【问题描述】:

我必须将 pandas 数据框写入 PostgreSQL 表。在这里你可以找到一段 sn-p 代码:

values = [1, 1]
d = {'col1': values, 'col2': values}
df = pd.DataFrame(data=d)
df.to_sql('prograd_dashboard', con=dbConnection, if_exists='replace', index=False,  method='multi')

我遇到了这个错误信息:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'dict' 'INSERT INTO prograd_dashboard (col1, col2) VALUES (%(col1)s, %(col2)s)' {'col1': {'col1': 1, 'col2': 1}, 'col2': {'col1': 1, 'col2': 1}}

如您所见,数据框中没有字典。然而,实际上在 sqlalchemy 生成的插入命令中有一个 dict。

如果我设置 method=None,代码就可以完美运行。但是,随着记录数量的增加,性能会下降。

我该如何解决这个问题? 谢谢!

【问题讨论】:

标签: python pandas sqlalchemy pandas-to-sql


【解决方案1】:

我通过更新 pandas 和 SQLAlchemy 库解决了这个问题。

【讨论】:

  • 您使用什么版本的 Pandas 作为 SQL Alchemy 进行修复?
  • 我目前使用的是1.3.24版本
猜你喜欢
  • 2017-10-28
  • 2021-08-08
  • 2019-11-10
  • 2019-12-21
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 2021-05-23
相关资源
最近更新 更多