query fails with local variables in expressions · Issue #5087 · pandas-dev/pandas (original) (raw)
Proper message for overlapping locals/frame (tested):
In [28]: a = Series(randint(3, size=5), name='a')
In [29]: b = Series(randint(10, size=5), name='b')
In [30]: df = DataFrame({'a': a, 'b': b})
In [31]: df.query('b - 1 in a')
NameResolutionError: resolvers and locals overlap on names ['b']
Expression works when there's no local (tested)
In [50]: del b
In [51]: a
Out[51]:
0 2
1 2
2 0
3 1
4 2
Name: a, dtype: int64
In [52]: df.b - 1
Out[52]:
0 3
1 4
2 7
3 7
4 -1
Name: b, dtype: int64
In [53]: df.query('b - 1 in a')
Out[53]:
Empty DataFrame
Columns: [a, b]
Index: []
Works fine when I use the local by itself (tested):
In [34]: df.query('@b in a')
Out[34]:
a b
4 2 0
However, when I try to reference the local b
using @b
in an expression, things go awry (not tested):
In [55]: b = Series(randint(10, size=5), name='b')
In [56]: df.query('@b - 1 in a')
UndefinedVariableError: local variable '@b' is not defined