Skip to content

Commit ef777e7

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

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pandas/core/computation/expr.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,13 @@ 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
422+
not right.is_scalar and right.return_type == f32):
422423
# right is a float32 array, left is a scalar
423424
name = self.env.add_tmp(np.float32(left.value))
424425
left = self.term_type(name, self.env)
425-
if right.is_scalar and not left.is_scalar and left.return_type == f32:
426+
if (right.is_scalar and hasattr(right, 'value') and
427+
not left.is_scalar and left.return_type == f32):
426428
# left is a float32 array, right is a scalar
427429
name = self.env.add_tmp(np.float32(right.value))
428430
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)