diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index 989b05003d76f..afb143ac78df1 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -94,7 +94,7 @@ Bug Fixes - +- Bug in ``pd.eval`` where unary ops in a list error (:issue:`11235`) - Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`) diff --git a/pandas/computation/expr.py b/pandas/computation/expr.py index 2ae6f29f74efc..6da5cf4753a8e 100644 --- a/pandas/computation/expr.py +++ b/pandas/computation/expr.py @@ -427,7 +427,7 @@ def visit_Str(self, node, **kwargs): return self.term_type(name, self.env) def visit_List(self, node, **kwargs): - name = self.env.add_tmp([self.visit(e).value for e in node.elts]) + name = self.env.add_tmp([self.visit(e)(self.env) for e in node.elts]) return self.term_type(name, self.env) visit_Tuple = visit_List @@ -655,7 +655,7 @@ def visitor(x, y): return reduce(visitor, operands) # ast.Call signature changed on 3.5, -# conditionally change which methods is named +# conditionally change which methods is named # visit_Call depending on Python version, #11097 if compat.PY35: BaseExprVisitor.visit_Call = BaseExprVisitor.visit_Call_35 diff --git a/pandas/computation/tests/test_eval.py b/pandas/computation/tests/test_eval.py index 8db0b82f1aa2e..7474c0d118612 100644 --- a/pandas/computation/tests/test_eval.py +++ b/pandas/computation/tests/test_eval.py @@ -29,7 +29,7 @@ import pandas.computation.expr as expr import pandas.util.testing as tm from pandas.util.testing import (assert_frame_equal, randbool, - assertRaisesRegexp, + assertRaisesRegexp, assert_numpy_array_equal, assert_produces_warning, assert_series_equal) from pandas.compat import PY3, u, reduce @@ -609,6 +609,16 @@ def test_scalar_unary(self): self.assertEqual( pd.eval('+False', parser=self.parser, engine=self.engine), +False) + def test_unary_in_array(self): + # GH 11235 + assert_numpy_array_equal( + pd.eval('[-True, True, ~True, +True,' + '-False, False, ~False, +False,' + '-37, 37, ~37, +37]'), + np.array([-True, True, ~True, +True, + -False, False, ~False, +False, + -37, 37, ~37, +37])) + def test_disallow_scalar_bool_ops(self): exprs = '1 or 2', '1 and 2' exprs += 'a and b', 'a or b' @@ -1256,6 +1266,13 @@ def f(): expected['c'] = expected['a'] + expected['b'] assert_frame_equal(df, expected) + def test_column_in(self): + # GH 11235 + df = DataFrame({'a': [11], 'b': [-32]}) + result = df.eval('a in [11, -32]') + expected = Series([True]) + assert_series_equal(result, expected) + def test_basic_period_index_boolean_expression(self): df = mkdf(2, 2, data_gen_f=f, c_idx_type='p', r_idx_type='i')