Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert sqlalchemy ORM query object to sql query for Pandas DataFrame

This question feels fiendishly simple but I haven't been able to find an answer.

I have an ORM query object, say

query_obj = session.query(Class1).join(Class2).filter(Class2.attr == 'state')

I can read it into a dataframe like so:

testdf = pd.read_sql(query_obj.statement, query_obj.session.bind)

But what I really want to do is use a traditional SQL query instead of the ORM:

with engine.connect() as connection:
    # Execute the query against the database
    results = connection.execute(query_obj)
    # Fetch all the results of the query
    fetchall = results.fetchall()
    # Build a DataFrame with the results
    dataframe = pd.DataFrame(fetchall)

Where query is a traditional SQL string. Now when I run this I get an error along the lines of "query_obj is not executable" Anyone know how to convert the ORM query to a traditional query? Also how does one get the columns in after getting the dataframe?

Context why I'm doing this: I've set up an ORM layer on top of my database and am using it to query data into a Pandas DataFrame. It works, but it's frequently maxing out my memory. I want to cut my in-memory overhead with some string folding (pass 3 outlined here: http://www.mobify.com/blog/sqlalchemy-memory-magic/). That requires (and correct me if I'm wrong here) not using the read_sql string and instead processing the query's return as raw tuples.

like image 817
AZhao Avatar asked Aug 05 '15 13:08

AZhao


People also ask

Does pandas use SQLAlchemy?

Since SQLAlchemy is integrated with Pandas, we can use its SQL connection directly with “con = conn”.

Is SQLAlchemy the same as SQL?

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

What does SQLAlchemy query return?

It returns an instance based on the given primary key identifier providing direct access to the identity map of the owning Session. It creates a SQL JOIN against this Query object's criterion and apply generatively, returning the newly resulting Query. It returns exactly one result or raise an exception.


2 Answers

The long version is described in detail in the FAQ of sqlalchemy: http://sqlalchemy.readthedocs.org/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined

The short version is:

statement = query.statement
print(statement.compile(engine))

The result of this can be used in read_sql.

like image 158
joris Avatar answered Nov 16 '22 06:11

joris


this may be a later version of sqlalchemy since the post.

print(query)

outputs the query you can copy and paste back into your script.

like image 45
brddawg Avatar answered Nov 16 '22 08:11

brddawg