Pandasql在一个基本示例中返回错误。

huangapple go评论55阅读模式
英文:

Pandasql returns error with a basic example

问题

以下是翻译的代码部分:

以下代码在运行时

    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)

出现以下错误

    输出超出大小限制在文本编辑器中打开完整的输出数据
    ---------------------------------------------------------------------------
    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'
    
    上述异常是以下异常的直接原因
    
    ObjectNotExecutableError                  Traceback (most recent call last)
    Cell In[1], line 11
          8 query = "SELECT * FROM df WHERE col1 > 2"
         10 # 使用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     使用SQL语法查询Pandas数据帧
        127     此函数仅用于向后兼容性。鼓励新用户使用PandaSQL类。
       (...)
        154     >>> sqldf("select avg(x) from df;", locals())
    ...
       1416         distilled_parameters,
       1417         execution_options or NO_OPTIONS,
       1418     )
    
    ObjectNotExecutableError: 不是可执行对象:'SELECT * FROM df WHERE col1 > 2'

请有人帮助我吗?
英文:

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?

答案1

得分: 0

问题可以通过降级 SQLAlchemy 来解决:

pip install SQLAlchemy==1.4.46

有关更多详细信息,请参阅 bug 报告

英文:

The problem could be fixed by downgrading SQLAlchemy:

pip install SQLAlchemy==1.4.46

See bug report for more details.

huangapple
  • 本文由 发表于 2023年2月6日 20:14:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定