SQLAlchemy AttributeError:“模块"对象没有属性"PandasSQLAlchemy" [英] SQLAlchemy AttributeError: 'module' object has no attribute 'PandasSQLAlchemy'
问题描述
我正在将熊猫数据框写入Postgres数据库:
I'm writing a pandas Dataframe to a Postgres database:
from sqlalchemy import create_engine, MetaData
engine = create_engine(r'postgresql://user:password@localhost:5432/db')
meta = MetaData(engine, schema='data_quality')
meta.reflect(engine, schema='data_quality')
pdsql = pd.io.sql.PandasSQLAlchemy(engine, meta=meta)
pdsql.to_sql(dataframe, table_name)
它运行良好,但是现在SQLAlchemy在第5行抛出以下错误:
It was working perfectly, but now SQLAlchemy is throwing the following error at the 5th line:
AttributeError: 'module' object has no attribute 'PandasSQLAlchemy'
我不确定是否相关,但是熊猫同时坏了-就像在此google-api-python-client问题中一样:
I'm not sure if it's related, but Pandas broke at the same time - exactly like in this google-api-python-client issue:
我昨天安装了google-api-python-client并将其卸载解决了Pandas的问题,但是SQLAlchemy仍然无法正常工作.
I installed the google-api-python-client yesterday and uninstalling it fixed the problem with Pandas, but SQLAlchemy still doesn't work.
推荐答案
我想您使用的是熊猫0.15. PandasSQLAlchemy
尚未真正公开,并在0.15中将熊猫重命名为SQLDatabase
.因此,如果您在代码中替换掉它,它应该可以工作(所以pdsql = pd.io.sql.SQLDatabase(engine, meta=meta)
).
I suppose you are using pandas 0.15. PandasSQLAlchemy
was not yet really public, and was renamed in pandas 0.15 to SQLDatabase
. So if you replace that in your code, it should work (so pdsql = pd.io.sql.SQLDatabase(engine, meta=meta)
).
但是,从熊猫0.15开始,在read_sql_table
和to_sql
函数中也提供了模式支持,因此不需要手动创建MetaData
和SQLDatabase
对象.相反,应该这样做:
However, starting from pandas 0.15, there is also schema support in the read_sql_table
and to_sql
functions, so it should not be needed to make a MetaData
and SQLDatabase
object manually. Instead, this should do it:
dataframe.to_sql(table_name, engine, schema='data_quality')
See the 0.15 release notes: http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#improvements-in-the-sql-io-module
这篇关于SQLAlchemy AttributeError:“模块"对象没有属性"PandasSQLAlchemy"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!