skip to Main Content

I need to pandasql library for doing some queries on my pandas dataframes. I have run the below code on a simple sample and got the following Error. Strangely, can’t consider it as a query!
I tried to reinstall, and upgrade pandasql, pandas, pip, and some others, but I couldn’t find what is the problem. I am using Jupiter notebook and the compiler is python3.8 on Ubuntu 20.04.5 LTS.

code:

import pandas as pd
import pandasql as ps
import numpy as np



df = pd.DataFrame([[1234, 'Customer A', '123 Street', np.nan],
               [1234, 'Customer A', np.nan, '333 Street'],
               [1233, 'Customer B', '444 Street', '333 Street'],
              [1233, 'Customer B', '444 Street', '666 Street']], columns=
['ID', 'Customer', 'Billing Address', 'Shipping Address'])

q1 = """SELECT * FROM df """

ps.sqldf(q1)

Result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py in 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)
<ipython-input-21-8b921e84766f> in <module>
     13 q1 = """SELECT * FROM df """
     14 
---> 15 ps.sqldf(q1)

~/.local/lib/python3.8/site-packages/pandasql/sqldf.py in sqldf(query, env, db_uri)
    154     >>> sqldf("select avg(x) from df;", locals())
    155     """
--> 156     return PandaSQL(db_uri)(query, env)

~/.local/lib/python3.8/site-packages/pandasql/sqldf.py in __call__(self, query, env)
     59 
     60             try:
---> 61                 result = read_sql(query, conn)
     62             except DatabaseError as ex:
     63                 raise PandaSQLException(ex)

~/.local/lib/python3.8/site-packages/pandas/io/sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
    588         )
    589     else:
--> 590         return pandas_sql.read_query(
    591             sql,
    592             index_col=index_col,

~/.local/lib/python3.8/site-packages/pandas/io/sql.py in read_query(self, sql, index_col, coerce_float, parse_dates, params, chunksize, dtype)
   1558         args = _convert_params(sql, params)
   1559 
-> 1560         result = self.execute(*args)
   1561         columns = result.keys()
   1562 

~/.local/lib/python3.8/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1403     def execute(self, *args, **kwargs):
   1404         """Simple passthrough to SQLAlchemy connectable"""
-> 1405         return self.connectable.execution_options().execute(*args, **kwargs)
   1406 
   1407     def read_table(

~/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py in execute(self, statement, parameters, execution_options)
   1410             meth = statement._execute_on_connection
   1411         except AttributeError as err:
-> 1412             raise exc.ObjectNotExecutableError(statement) from err
   1413         else:
   1414             return meth(

ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df '

2

Answers


  1. This might be because of breaking changes in sqlalchemy 2.0. A SQL query must now be wrapped in a sqlalchemy.text() function to make it executable.

    It would be helpful to know your current sqlalchemy version to know if this is the issue you face.

    Login or Signup to reply.
  2. I was able to get this to work after downgrading SQLAlchemy to version 1.4.46,

    pip install sqlalchemy==1.4.46
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search