The Python Oracle

SQLAlchemy ORM conversion to pandas DataFrame

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Sqlalchemy Orm Conversion To Pandas Dataframe
00:53 Accepted Answer Score 269
01:09 Answer 2 Score 6
01:23 Answer 3 Score 152
01:43 Answer 4 Score 22
01:59 Thank you

--

Full question
https://stackoverflow.com/questions/2952...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #sqlalchemy #flasksqlalchemy

#avk47



ACCEPTED ANSWER

Score 269


Below should work in most cases:

df = pd.read_sql(query.statement, query.session.bind)

See pandas.read_sql documentation for more information on the parameters.




ANSWER 2

Score 152


Just to make this more clear for novice pandas programmers, here is a concrete example,

pd.read_sql(session.query(Complaint).filter(Complaint.id == 2).statement,session.bind) 

Here we select a complaint from complaints table (sqlalchemy model is Complaint) with id = 2




ANSWER 3

Score 22


The selected solution didn't work for me, as I kept getting the error

AttributeError: 'AnnotatedSelect' object has no attribute 'lower'

I found the following worked:

df = pd.read_sql_query(query.statement, engine)



ANSWER 4

Score 6


If you want to compile a query with parameters and dialect specific arguments, use something like this:

c = query.statement.compile(query.session.bind)
df = pandas.read_sql(c.string, query.session.bind, params=c.params)