Skip to content

Commit 59b77ed

Browse files
committed
Fix pandas-dev#16363: Prevent visit_BinOp from accessing value on UnaryOp
1 parent 882961d commit 59b77ed

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pandas/core/computation/expr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,11 @@ def _maybe_transform_eq_ne(self, node, left=None, right=None):
418418

419419
def _maybe_downcast_constants(self, left, right):
420420
f32 = np.dtype(np.float32)
421-
if left.is_scalar and not right.is_scalar and right.return_type == f32:
421+
if left.is_scalar and hasattr(left, 'value') and not right.is_scalar and right.return_type == f32:
422422
# right is a float32 array, left is a scalar
423423
name = self.env.add_tmp(np.float32(left.value))
424424
left = self.term_type(name, self.env)
425-
if right.is_scalar and not left.is_scalar and left.return_type == f32:
425+
if right.is_scalar and hasattr(right, 'value') and not left.is_scalar and left.return_type == f32:
426426
# left is a float32 array, right is a scalar
427427
name = self.env.add_tmp(np.float32(right.value))
428428
right = self.term_type(name, self.env)

pandas/tests/computation/test_eval.py

+6
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@ def test_unary_in_array(self):
608608
-False, False, ~False, +False,
609609
-37, 37, ~37, +37], dtype=np.object_))
610610

611+
def test_float_comparison_bin_op(self):
612+
# GH 16363
613+
df = pd.DataFrame({'x': np.array([0], dtype=np.float32)})
614+
res = df.eval('x < -0.1')
615+
assert np.array_equal(res, np.array([False])), res
616+
611617
def test_disallow_scalar_bool_ops(self):
612618
exprs = '1 or 2', '1 and 2'
613619
exprs += 'a and b', 'a or b'

0 commit comments

Comments
 (0)