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 does not work in a function

See original GitHub issue

Hi, I made a function like the one below to query a panda dataframe but it tells me ‘Exception: UNECE_csv not found’. Cannot understand where is the prob since if I run the same scripts outside the function it works perfectly.

def import_csv_and_query():

filepath='C:\\DATA\\Work\\.....csv
import pandas as pd
from pandasql import sqldf
UNECE_csv=pd.read_csv(filepath,dtype=object,na_filter=False)
q="""

MY LONG QUERY
"""

UNECE_clean=sqldf(q, globals())

UNECE_colums=UNECE_clean.columns.tolist()
UNECE_array=UNECE_clean.values.tolist()
UNECE=[UNECE_colums]+UNECE_array   
return UNECE

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
stonebigcommented, Jan 6, 2016

target variable are the one you want to query. Example:

import pandas as pd
import numpy as np
from pandasql import sqldf
global_df= pd.DataFrame({ 'A' : pd.Series(1,index=list(range(888)),dtype='float32'),
                     'B' : pd.Series(1,index=list(range(888)),dtype='float32')})
def rien(q, ext_df):
    local_df= pd.DataFrame({ 'B' : pd.Series(1,index=list(range(888)),dtype='float32'),
                     'D' : pd.Series(1,index=list(range(0, 888, 2)),dtype='float32')})
    return sqldf(q, {'ext_df':ext_df, 'local_df':local_df})

rien("select * FROM ext_df INNER JOIN local_df on ext_df.B=local_df.B limit 3", global_df) 
0reactions
TheGlobalistcommented, Aug 14, 2020

target variable are the one you want to query. Example:

import pandas as pd
import numpy as np
from pandasql import sqldf
global_df= pd.DataFrame({ 'A' : pd.Series(1,index=list(range(888)),dtype='float32'),
                     'B' : pd.Series(1,index=list(range(888)),dtype='float32')})
def rien(q, ext_df):
    local_df= pd.DataFrame({ 'B' : pd.Series(1,index=list(range(888)),dtype='float32'),
                     'D' : pd.Series(1,index=list(range(0, 888, 2)),dtype='float32')})
    return sqldf(q, {'ext_df':ext_df, 'local_df':local_df})

rien("select * FROM ext_df INNER JOIN local_df on ext_df.B=local_df.B limit 3", global_df) 

sorry for posting on a 4 year old issue, but just wanted to highlight that the proposed solution also worked for me using Sanic. Just wanted to leave this here so that others could know it

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandasql is not working in a function python pandas
I have tried to use locals() instead, but it's still not working. it would be appreciated for any input or advice on how...
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 >
Introduction to PandaSQL: The Downsides No One Talks About
We create a lambda function that takes a query q and the variables in the scope of the program using globals() which returns...
Read more >
[Solved]-Pandasql: sqldf executing error-Pandas,Python
Coding example for the question Pandasql: sqldf executing error-Pandas,Python. ... Python - Pandasql - IIf Function doesn't work Error · Error with query...
Read more >
pandasql · PyPI
It works similarly to sqldf in R. pandasql seeks to provide a more familiar ... 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