Skip to content

Commit 22d75a3

Browse files
committed
Merge pull request #11366 from kawochen/BUG-FIX-11235
BUG: GH11235 where pd.eval doesn't handle unary ops in lists
2 parents 37a80bc + f3d8df7 commit 22d75a3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v0.17.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Bug Fixes
9595

9696

9797

98-
98+
- Bug in ``pd.eval`` where unary ops in a list error (:issue:`11235`)
9999
- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`)
100100

101101

pandas/computation/expr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def visit_Str(self, node, **kwargs):
427427
return self.term_type(name, self.env)
428428

429429
def visit_List(self, node, **kwargs):
430-
name = self.env.add_tmp([self.visit(e).value for e in node.elts])
430+
name = self.env.add_tmp([self.visit(e)(self.env) for e in node.elts])
431431
return self.term_type(name, self.env)
432432

433433
visit_Tuple = visit_List
@@ -655,7 +655,7 @@ def visitor(x, y):
655655
return reduce(visitor, operands)
656656

657657
# ast.Call signature changed on 3.5,
658-
# conditionally change which methods is named
658+
# conditionally change which methods is named
659659
# visit_Call depending on Python version, #11097
660660
if compat.PY35:
661661
BaseExprVisitor.visit_Call = BaseExprVisitor.visit_Call_35

pandas/computation/tests/test_eval.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import pandas.computation.expr as expr
3030
import pandas.util.testing as tm
3131
from pandas.util.testing import (assert_frame_equal, randbool,
32-
assertRaisesRegexp,
32+
assertRaisesRegexp, assert_numpy_array_equal,
3333
assert_produces_warning, assert_series_equal)
3434
from pandas.compat import PY3, u, reduce
3535

@@ -609,6 +609,16 @@ def test_scalar_unary(self):
609609
self.assertEqual(
610610
pd.eval('+False', parser=self.parser, engine=self.engine), +False)
611611

612+
def test_unary_in_array(self):
613+
# GH 11235
614+
assert_numpy_array_equal(
615+
pd.eval('[-True, True, ~True, +True,'
616+
'-False, False, ~False, +False,'
617+
'-37, 37, ~37, +37]'),
618+
np.array([-True, True, ~True, +True,
619+
-False, False, ~False, +False,
620+
-37, 37, ~37, +37]))
621+
612622
def test_disallow_scalar_bool_ops(self):
613623
exprs = '1 or 2', '1 and 2'
614624
exprs += 'a and b', 'a or b'
@@ -1256,6 +1266,13 @@ def f():
12561266
expected['c'] = expected['a'] + expected['b']
12571267
assert_frame_equal(df, expected)
12581268

1269+
def test_column_in(self):
1270+
# GH 11235
1271+
df = DataFrame({'a': [11], 'b': [-32]})
1272+
result = df.eval('a in [11, -32]')
1273+
expected = Series([True])
1274+
assert_series_equal(result, expected)
1275+
12591276
def test_basic_period_index_boolean_expression(self):
12601277
df = mkdf(2, 2, data_gen_f=f, c_idx_type='p', r_idx_type='i')
12611278

0 commit comments

Comments
 (0)