Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqldf does not work #103

Open
psads-git opened this issue Feb 6, 2023 · 3 comments
Open

sqldf does not work #103

psads-git opened this issue Feb 6, 2023 · 3 comments

Comments

@psads-git
Copy link

The following code when run

import pandas as pd
from pandasql import sqldf

df = pd.DataFrame({'col1': [1, 2, 3, 4], 'col2': [10, 20, 30, 40]})

query = "SELECT * FROM df WHERE col1 > 2"

result = sqldf(query, globals())
print(result)

gives the following error:

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/sqlalchemy/engine/base.py:1410, in Connection.execute(self, statement, parameters, execution_options)
   1409 try:
-> 1410     meth = statement._execute_on_connection
   1411 except AttributeError as err:

AttributeError: 'str' object has no attribute '_execute_on_connection'

The above exception was the direct cause of the following exception:

ObjectNotExecutableError                  Traceback (most recent call last)
Cell In[1], line 11
      8 query = "SELECT * FROM df WHERE col1 > 2"
     10 # Execute the query using pandasql
---> 11 result = sqldf(query, globals())
     13 print(result)

File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/pandasql/sqldf.py:156, in sqldf(query, env, db_uri)
    124 def sqldf(query, env=None, db_uri='sqlite:///:memory:'):
    125     """
    126     Query pandas data frames using sql syntax
    127     This function is meant for backward compatibility only. New users are encouraged to use the PandaSQL class.
   (...)
    154     >>> sqldf("select avg(x) from df;", locals())
...
   1416         distilled_parameters,
   1417         execution_options or NO_OPTIONS,
   1418     )

ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df WHERE col1 > 2'

Could someone please help me?

@Isaac-Flath
Copy link

Isaac-Flath commented Feb 7, 2023

I just debugged this earlier today for a coworker and found downgrading SQLAlchemy resolved it via pip install SQLAlchemy==1.4.46.

I think pandasql is not compatible with SQLalchemy 2 and above.

See #102

@psads-git
Copy link
Author

Thanks, Isaac: Indeed, by downgrading SQLAlchemy to the version you suggest fixes the problem.

@hrshdhgd
Copy link

If you want to use SQLAlchemy v2.x, I think merging my PR will resolve this and 2 other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants