title: Function containing pandas query can't find local variable only if imported
body: <p><strong>EDIT Before BOUNTY:</strong></p>
  
  <p>I would like to have a function where I can write <code>subset(pd.dataframe,query="col %in% list_with_substrings")</code></p>
  
  <p>Where it returns the same dataframe where only rows contains any of the substrings in <code>list_with_substrings</code> for the <code>col</code>.</p>
  
  <p>As if i had written:  <code>dataframe.query("col in @list_with_substring")</code></p>
  
  <p>And it should be possible to import that function into another script, and not have to redefine the function everytime it is used. If redefining is the only option, then it should be done inside the function itself. So that the subset call is a single line. </p>
  
  <p><strong>Original Post:</strong></p>
  
  <p>I have two scripts:</p>
  
  <p>"dataprocessing.py" </p>
  
  <pre><code>import pandas as pd
  def subset(df,query):
      query = query.replace("%in%", "in @")
      query = query.replace("%!in%", "not in @")     
      return pd.DataFrame(df.query(query))
  </code></pre>
  
  <p>and "test_dataprocessing.py"</p>
  
  <pre><code>from dataprocessing import *
  
  df = pd.DataFrame({'countries':['US','UK','GE','Ch',"DK","SW"]})
  countries_to_subset = ['UK','CH']
  
  subset(df,query="countries %in% countries_to_subset")
  </code></pre>
  
  <p>This produces the error: </p>
  
  <p>pandas.core.computation.ops.UndefinedVariableError: local variable 'countries_to_subset' is not defined</p>
  
  <p>but if I then define the function inside the same script </p>
  
  <pre><code>def subset(df,query):
      query = query.replace("%in%", "in @")
      query = query.replace("%!in%", "not in @")
      return pd.DataFrame(df.query(query))
  
  subset(df,query="countries %in% countries_to_subset")
  
  Out: 
    countries  GDP
  1        UK    2
  3     China    4
  </code></pre>
  
  <p>So I can't import a function that uses query and pass it a local variable?
  Is there a way to import the subset function "as if" it was defined in the same script?</p>
  
  <p>tested on Python 3.6 &amp; 3.7 and pandas 0.23.0 &amp; 0.24.2</p>