question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PandaSQL won't run inside main()

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13

github_iconTop GitHub Comments

8reactions
dylanhoggcommented, Jul 14, 2017

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.

2reactions
loscil06commented, Aug 13, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found