`pd.read_sql(sql, engine)` 引发 NotImplementedError:此方法未针对 SQLAlchemy 2.0 实现

Haj*_*zip 7 python sqlalchemy pandas read-sql

我尝试使用 sqlalchemy 引擎直接从 sqlserver 数据库创建 pandas DataFrame:

engine = create_engine(URL_string, echo=False, future=True)
query_string = "..."
dt = pd.read_sql(query_string, engine)
Run Code Online (Sandbox Code Playgroud)

但这会引发此错误:

File <redacted>/venv/lib/python3.8/site-packages/sqlalchemy/future/engine.py:320, in Engine._not_implemented(self, *arg, **kw)
    319 def _not_implemented(self, *arg, **kw):
--> 320     raise NotImplementedError(
    321         "This method is not implemented for SQLAlchemy 2.0."
    322     )

NotImplementedError: This method is not implemented for SQLAlchemy 2.0.
Run Code Online (Sandbox Code Playgroud)

我这样做是因为单独使用 pyodbc 的连接会发出警告:

UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy
  warnings.warn(
Run Code Online (Sandbox Code Playgroud)

我正在使用 sqlalchemy 版本 1.4 ...那么我该如何解决这个问题?

Haj*_*zip 4

future=True只需从参数中删除engine

engine = create_engine(URL_string, echo=False)
Run Code Online (Sandbox Code Playgroud)

那么你就可以出发了!