Created
July 3, 2017 14:19
-
-
Save jakebrinkmann/de7fd185efe9a1f459946cf72def057e to your computer and use it in GitHub Desktop.
Read SQL query from psycopg2 into pandas dataframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import pandas.io.sql as sqlio | |
import psycopg2 | |
conn = psycopg2.connect("host='{}' port={} dbname='{}' user={} password={}".format(host, port, dbname, username, pwd)) | |
sql = "select count(*) from table;" | |
dat = sqlio.read_sql_query(sql, conn) | |
conn = None |
does anyone now if pd.read_sql blocks? im have a multi-thread process that uses it, and it seems to block
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
> The example does not work for me: `UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql_query(sql, conn)
It should actually work and extract your table though because it's just a UserWarning, a warning that you can ignore (but it's not recommended to ignore it)
In Pandas, Psycopg2 is not tested as the warning implies, and it prefers using SQLAlchemy instead.
You can remake the example above by running the following code:
You can actually create an engine by passing a URL object instead of the string:
I'm not an expert but I hope it helps!