Skip to content

BUG: GH11235 where pd.eval doesn't handle unary ops in lists #11366

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

Merged
merged 1 commit into from
Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)


Expand Down
4 changes: 2 additions & 2 deletions pandas/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion pandas/computation/tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add in the op original example as well (could locate where some of other eval tests are, in test_frame IIRC)

add in the issue number here as well


def test_disallow_scalar_bool_ops(self):
exprs = '1 or 2', '1 and 2'
exprs += 'a and b', 'a or b'
Expand Down Expand Up @@ -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')

Expand Down