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

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

Pandasql returns error with a basic example

问题

以下是翻译的代码部分:

  1. 以下代码在运行时
  2. import pandas as pd
  3. from pandasql import sqldf
  4. df = pd.DataFrame({'col1': [1, 2, 3, 4], 'col2': [10, 20, 30, 40]})
  5. query = "SELECT * FROM df WHERE col1 > 2"
  6. result = sqldf(query, globals())
  7. print(result)
  8. 出现以下错误
  9. 输出超出大小限制在文本编辑器中打开完整的输出数据
  10. ---------------------------------------------------------------------------
  11. AttributeError Traceback (most recent call last)
  12. File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/sqlalchemy/engine/base.py:1410, in Connection.execute(self, statement, parameters, execution_options)
  13. 1409 try:
  14. -> 1410 meth = statement._execute_on_connection
  15. 1411 except AttributeError as err:
  16. AttributeError: 'str' object has no attribute '_execute_on_connection'
  17. 上述异常是以下异常的直接原因
  18. ObjectNotExecutableError Traceback (most recent call last)
  19. Cell In[1], line 11
  20. 8 query = "SELECT * FROM df WHERE col1 > 2"
  21. 10 # 使用pandasql执行查询
  22. ---> 11 result = sqldf(query, globals())
  23. 13 print(result)
  24. File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/pandasql/sqldf.py:156, in sqldf(query, env, db_uri)
  25. 124 def sqldf(query, env=None, db_uri='sqlite:///:memory:'):
  26. 125 """
  27. 126 使用SQL语法查询Pandas数据帧
  28. 127 此函数仅用于向后兼容性。鼓励新用户使用PandaSQL类。
  29. (...)
  30. 154 >>> sqldf("select avg(x) from df;", locals())
  31. ...
  32. 1416 distilled_parameters,
  33. 1417 execution_options or NO_OPTIONS,
  34. 1418 )
  35. ObjectNotExecutableError: 不是可执行对象:'SELECT * FROM df WHERE col1 > 2'
  36. 请有人帮助我吗?
英文:

The following code when run

  1. import pandas as pd
  2. from pandasql import sqldf
  3. df = pd.DataFrame({'col1': [1, 2, 3, 4], 'col2': [10, 20, 30, 40]})
  4. query = "SELECT * FROM df WHERE col1 > 2"
  5. result = sqldf(query, globals())
  6. print(result)

gives the following error:

  1. Output exceeds the size limit. Open the full output data in a text editor
  2. ---------------------------------------------------------------------------
  3. AttributeError Traceback (most recent call last)
  4. File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/sqlalchemy/engine/base.py:1410, in Connection.execute(self, statement, parameters, execution_options)
  5. 1409 try:
  6. -> 1410 meth = statement._execute_on_connection
  7. 1411 except AttributeError as err:
  8. AttributeError: 'str' object has no attribute '_execute_on_connection'
  9. The above exception was the direct cause of the following exception:
  10. ObjectNotExecutableError Traceback (most recent call last)
  11. Cell In[1], line 11
  12. 8 query = "SELECT * FROM df WHERE col1 > 2"
  13. 10 # Execute the query using pandasql
  14. ---> 11 result = sqldf(query, globals())
  15. 13 print(result)
  16. File ~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/pandasql/sqldf.py:156, in sqldf(query, env, db_uri)
  17. 124 def sqldf(query, env=None, db_uri='sqlite:///:memory:'):
  18. 125 """
  19. 126 Query pandas data frames using sql syntax
  20. 127 This function is meant for backward compatibility only. New users are encouraged to use the PandaSQL class.
  21. (...)
  22. 154 >>> sqldf("select avg(x) from df;", locals())
  23. ...
  24. 1416 distilled_parameters,
  25. 1417 execution_options or NO_OPTIONS,
  26. 1418 )
  27. ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df WHERE col1 > 2'

Could someone please help me?

答案1

得分: 0

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

  1. pip install SQLAlchemy==1.4.46

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

英文:

The problem could be fixed by downgrading SQLAlchemy:

  1. 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:

确定