gpt4 book ai didi

python - pandas to_sql() 给出了 SADeprecationWarning

转载 作者:行者123 更新时间:2023-12-04 12:15:50 28 4
gpt4 key购买 nike

to_sql() pandas 中的函数现在正在产生 SADeprecationWarning。df.to_sql(name=tablename, con=c, if_exists='append', index=False )

[..]/lib/python3.8/site-packages/pandas/io/sql.py:1430: SADeprecationWarning:The Connection.run_callable() method is deprecated and will be removed in a future release.  Use a context manager instead. (deprecated since: 1.4)
即使使用 df.read_sql() 我也得到了这个命令,在运行 sql select 语句时。改成原来的 df.read_sql_query()它环绕,摆脱它。我怀疑那里会有一些联系。
所以,问题是,如何将数据帧表写入 SQL 而不会在 future 版本中被弃用? “使用上下文管理器”是什么意思,我该如何实现?
版本:
Pandas :1.1.5 | SQLAlchemy: 1.4.0 | pyodbc: 4.0.30 | python :3.8.0
使用 mssql 数据库。
操作系统:Linux Mint Xfce,18.04。使用python虚拟环境。
如果重要,连接创建如下:
conn_str = r'mssql+pyodbc:///?odbc_connect={}'.format(dbString).strip()
sqlEngine = sqlalchemy.create_engine(conn_str,echo=False, pool_recycle=3600)
c = sqlEngine.connect()
而在db操作之后,
c.close()
这样做可以让主连接 sqlEngine 在 api 调用之间保持“事件”状态,并让我使用池连接而不必重新连接。

最佳答案

更新:据 Pandas 团队称,这将在截至撰写本文时尚未发布的 Pandas 1.2.4 中修复。
添加此作为答案,因为谷歌在这里领先,但接受的答案不适用。
我们使用 Pandas 的周围代码确实使用了上下文管理器:

with get_engine(dbname).connect() as conn:
df = pd.read_sql(stmt, conn, **kwargs)
return df
就我而言,这个错误是从 Pandas 本身内部抛出的,而不是在使用 Pandas 的周围代码中:
  /Users/tommycarpenter/Development/python-indexapi/.tox/py37/lib/python3.7/site-packages/pandas/io/sql.py:1430: SADeprecationWarning: The Engine.run_callable() method is deprecated and will be removed in a future release.  Use the Engine.connect() context manager instead. (deprecated since: 1.4)
Pandas 本身的片段是:
def has_table(self, name, schema=None):
return self.connectable.run_callable(
self.connectable.dialect.has_table, name, schema or self.meta.schema
)
我提出了一个问题: https://github.com/pandas-dev/pandas/issues/40825

关于python - pandas to_sql() 给出了 SADeprecationWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66680224/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com