The Python Oracle

SQLAlchemy ORM conversion to pandas DataFrame

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation

--

Chapters
00:00 Question
01:04 Accepted answer (Score 257)
01:23 Answer 2 (Score 144)
01:47 Answer 3 (Score 29)
03:01 Answer 4 (Score 19)
03:24 Thank you

--

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

Accepted answer links:
[pandas.read_sql]: http://pandas.pydata.org/pandas-docs/sta...

Answer 3 links:
[read_sql_query()]: https://pandas.pydata.org/docs/reference...
[from_records()]: https://pandas.pydata.org/docs/reference...

--

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)