SQLAlchemy AttributeError: 'module' object has no attribute 'PandasSQLAlchemy'

I am writing pandas Dataframe to 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)

      

It worked fine, but now SQLAlchemy is throwing the following error on the 5th line:

AttributeError: 'module' object has no attribute 'PandasSQLAlchemy'

      

I'm not sure if this is related, but pandas broke at the same time - just like in this google-api-python-client issue:

Failed to import Pandas: TypeError

Yesterday I installed google-api-python-client and uninstalling it fixed the Pandas issue, but SQLAlchemy still doesn't work.

+1


source to share


1 answer


Let's assume you are using pandas 0.15. PandasSQLAlchemy

was not yet publicly available and has been renamed pandas from 0.15 to SQLDatabase

. So if you replace that in your code, it should work (sic pdsql = pd.io.sql.SQLDatabase(engine, meta=meta)

).

However, since pandas 0.15, there is a support scheme in the functions read_sql_table

and to_sql

, therefore, do not need to create objects MetaData

and SQLDatabase

manually. Instead, this should do this:



dataframe.to_sql(table_name, engine, schema='data_quality')

      

See release notes for 0.15: http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#improvements-in-the-sql-io-module

+1


source







All Articles