Asked By – Jared
Is there a solution converting a SQLAlchemy
<Query object> to a pandas DataFrame?
Pandas has the capability to use
pandas.read_sql but this requires use of raw SQL. I have two reasons for wanting to avoid it:
- I already have everything using the ORM (a good reason in and of itself) and
- I’m using python lists as part of the query, e.g.:
Itemis my model class and
add_symbolsis a list). This is the equivalent of SQL
SELECT ... from ... WHERE ... IN.
Is anything possible?
Now we will see solution for issue: SQLAlchemy ORM conversion to pandas DataFrame
Below should work in most cases:
df = pd.read_sql(query.statement, query.session.bind)
pandas.read_sql documentation for more information on the parameters.
This question is answered By – van
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0