Pandas writing dataframe to other postgresql schema

后端 未结 2 771
南方客
南方客 2020-12-15 07:35

I am trying to write a pandas DataFrame to a PostgreSQL database, using a schema-qualified table.

I use the following code:

import pandas.io.sql as p         


        
相关标签:
2条回答
  • 2020-12-15 07:48

    Update: starting from pandas 0.15, writing to different schema's is supported. Then you will be able to use the schema keyword argument:

    df.to_sql('test', engine, schema='a_schema')
    

    Writing to different schema's is not yet supported at the moment with the read_sql and to_sql functions (but an enhancement request has already been filed: https://github.com/pydata/pandas/issues/7441).

    However, you can get around for now using the object interface with PandasSQLAlchemy and providing a custom MetaData object:

    meta = sqlalchemy.MetaData(engine, schema='a_schema')
    meta.reflect()
    pdsql = pd.io.sql.PandasSQLAlchemy(engine, meta=meta)
    pdsql.to_sql(df, 'test')
    

    Beware! This interface (PandasSQLAlchemy) is not yet really public and will still undergo changes in the next version of pandas, but this is how you can do it for pandas 0.14.

    Update: PandasSQLAlchemy is renamed to SQLDatabase in pandas 0.15.

    0 讨论(0)
  • 2020-12-15 08:07

    Solved, thanks to joris answer. Code was also improved thanks to joris comment, by passing around sqlalchemy engine instead of connection objects.

    import pandas as pd
    from sqlalchemy import create_engine, MetaData
    
    engine = create_engine(r'postgresql://some:user@host/db')
    meta = sqlalchemy.MetaData(engine, schema='a_schema')
    meta.reflect(engine, schema='a_schema')
    pdsql = pd.io.sql.PandasSQLAlchemy(engine, meta=meta)
    
    df = pd.read_sql("SELECT * FROM xxx", con=engine)    
    pdsql.to_sql(df, 'test')
    
    0 讨论(0)
提交回复
热议问题