PandaSQL won't run inside main()
See original GitHub issueHi there,
I noticed that when I attempt to call pandasql inside main()
, the script throws an error, no matter if I use globals()
or locals()
. I also tried to define qdf()
outside main()
, to no avail.
Code
# encoding=utf-8
# pandasql_test.py
# description:
# test for pandasql functionality
# inside a main() function
# imports
import pandas as pd
import pandasql as psql
# main function
def main():
# abbreviate pandasql functionality
qdf = lambda q: psql.sqldf(q, locals())
# create data
df = pd.DataFrame({
'name': ['Joe', 'Bill', 'Bob'],
'weight': [70, 85, 75]
})
q = 'select * from df'
print qdf(q)
if __name__ == '__main__':
main()
Error
pandasql.sqldf.PandaSQLException: (sqlite3.OperationalError) no such table: df [SQL: ‘select * from df’]
Thanks in advance for any advice!
Issue Analytics
- State:
- Created 7 years ago
- Comments:13
Top Results From Across the Web
pandasql is not working in a function python pandas
The code below seems to work for me. from pandasql import sqldf def select_sql(df_a): return sqldf("select * from df_a", locals()) df = pd....
Read more >Pandasql -The Best Way to Run SQL Queries in Python
In this article, we will see the best way to run SQL queries and code in python. we will also explore pandasql library...
Read more >Pandasql — Interesting Way To Run SQL Queries In Python
while using a Jupyter-Notebook type below line in a cell and hit Shift + Enter . !pip install pandasql. The main function in...
Read more >pandasql: Make python speak SQL - Alteryx Community
In this introduction, we'll show you to get up and running with pandasql inside of Rodeo, the integrated development environment (IDE) we ...
Read more >SQL with Pandas Data Frames - Predictive Hacks
We will work with the pandasql package and we can download it as follows: ... The main function used in pandasql is sqldf...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you leave out the lambda helper function
qdf
and callpsql.sqldf
directly it works withinmain()
print(psql.sqldf(q, locals()))
While annoying, at least there is a work around.
This happened to me recently irregardless of using locals() or globals(), what I did was to merge both locals() and globals() and it worked, like this:
sqldf(sql_string, {**locals(), **globals()})