Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: query with local strings gives KeyError #7300

Closed
jorisvandenbossche opened this issue May 31, 2014 · 1 comment · Fixed by #7303
Closed

BUG: query with local strings gives KeyError #7300

jorisvandenbossche opened this issue May 31, 2014 · 1 comment · Fixed by #7303
Labels
Bug Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@jorisvandenbossche
Copy link
Member

From SO question (http://stackoverflow.com/questions/23974664/unable-to-query-a-local-variable-in-pandas-0-14-0), referencing a local variable gives an error when this is a string (workaround seems to be to wrap it in a list):

In [39]: fills = pd.DataFrame({'Symbol':['BUD US', 'BUD US', 'IBM US', 'IBM US'], 'Price':[109.70, 109.72, 183.30, 183.35]})

In [40]: fills
Out[40]: 
    Price  Symbol
0  109.70  BUD US
1  109.72  BUD US
2  183.30  IBM US
3  183.35  IBM US

In [41]: my_symbol = ['BUD US']

In [42]: fills.query('Symbol==@my_symbol')
Out[42]: 
    Price  Symbol
0  109.70  BUD US
1  109.72  BUD US

In [43]: my_symbol = 'BUD US'

In [44]: fills.query('Symbol==@my_symbol')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-44-c16846018a13> in <module>()
----> 1 fills.query('Symbol==@my_symbol')

...

/home/joris/scipy/pandas/pandas/computation/expr.pyc in _rewrite_membership_op(self, node, left, right)
    341             # of one string, kind of a hack
    342             if right_str:
--> 343                 self.env.remove_tmp(right.name)
    344                 name = self.env.add_tmp([right.value])
    345                 right = self.term_type(name, self.env)

/home/joris/scipy/pandas/pandas/computation/scope.pyc in remove_tmp(self, name)
    287             The name of a temporary to be removed
    288         """
--> 289         del self.temps[name]
    290 
    291     @property

KeyError: '__pd_eval_local_my_symbol'
> /home/joris/scipy/pandas/pandas/computation/scope.py(289)remove_tmp()
    288         """
--> 289         del self.temps[name]
    290 

@cpcloud

@cpcloud
Copy link
Member

cpcloud commented May 31, 2014

Darn I totally thought that I had written a test for this. Guess not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants