pandasto_sql中“意外关键字参数‘fetch’”错误的来源?

我正在尝试将数据帧上传到 Heroku postgreSQL 服务器,我之前已经成功完成了几次。

这是我的代码,for_db我的 Pandas 数据框的名称在哪里:

from sqlalchemy import create_engine 

engine = create_engine('postgresql://wgam{rest of url}',
                       echo=False)
  
# attach the data frame to the sql server 
for_db.to_sql('phil_nlp', 
               con = engine,
              if_exists='replace') 

起初,它无法连接,因为 Heroku 给我的服务器 URL 一开始只有“postgres”,但我知道必须将其更改为“postgresql”才能正常工作,并且已经克服了最初的错误。

现在我收到一个新错误。

/usr/local/lib/python3.7/dist-packages/sqlalchemy/dialects/postgresql/psycopg2.py in do_executemany(self, cursor, statement, parameters, context)
    899                 template=executemany_values,
    900                 fetch=bool(context.compiled.returning),
--> 901                 **kwargs
    902             )
    903 

TypeError: execute_values() got an unexpected keyword argument 'fetch'

我不明白为什么会出现这种情况。显然我从未指定过这样的关键字参数。我已经做了很多搜索,但没有任何好的结果。任何人都知道为什么它现在会在上周工作的代码中抛出这个错误?

回答

我在运行该DataFrame.to_sql方法时遇到了同样的问题。添加method='multi'确实可以使其正常工作,并且是一个很好的解决方法。

进一步调查发现是我安装的 sqlalchemy 和 psycopg2 版本的问题。此处和此处的这些 github 问题使我想到了以下问题。

fetch参数是在psycopg2 版本 2.8上添加的。我有 2.7 版和 sqlalchemy 1.4.15

安装较新版本即可解决问题,无需添加method='multi'参数。

pip install psycopg2-binary==2.8.6

希望这可以帮助其他人发现这个问题


以上是pandasto_sql中“意外关键字参数‘fetch’”错误的来源?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>