pandasql: PandaSQL won't run inside main()

Hi 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!

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 15

Most upvoted comments

If you leave out the lambda helper function qdf and call psql.sqldf directly it works within main()

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()})