pandasto_sql()给出了SADeprecationWarning
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 而不会在未来版本中被弃用?“使用上下文管理器”是什么意思,我该如何实现?
版本:
熊猫: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 1.2.4 中修复。
添加此作为答案,因为谷歌在这里领先,但接受的答案不适用。
我们使用 Pandas 的周围代码确实使用了上下文管理器:
with get_engine(dbname).connect() as conn:
df = pd.read_sql(stmt, conn, **kwargs)
return df
就我而言,这个错误是从熊猫本身内部抛出的,而不是在使用熊猫的周围代码中:
/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)
熊猫本身的片段是:
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
THE END
二维码