Return Pandas dataframe from PostgreSQL query with sqlalchemy

后端 未结 3 1943
走了就别回头了
走了就别回头了 2021-01-30 10:08

I want to query a PostgreSQL database and return the output as a Pandas dataframe.

I created a connection to the database with \'SqlAlchemy\':

from sqlal         


        
3条回答
  •  不知归路
    2021-01-30 10:36

    You are bitten by the case (in)sensitivity issues with PostgreSQL. If you quote the table name in the query, it will work:

    df = pd.read_sql_query('select * from "Stat_Table"',con=engine)
    

    But personally, I would advise to just always use lower case table names (and column names), also when writing the table to the database to prevent such issues.


    From the PostgreSQL docs (http://www.postgresql.org/docs/8.0/static/sql-syntax.html#SQL-SYNTAX-IDENTIFIERS):

    Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case

    To explain a bit more: you have written a table with the name Stat_Table to the database (and sqlalchemy will quote this name, so it will be written as "Stat_Table" in the postgres database). When doing the query 'select * from Stat_Table' the unquoted table name will be converted to lower case stat_table, and so you get the message that this table is not found.

    See eg also Are PostgreSQL column names case-sensitive?

提交回复
热议问题