Skip to content

Commit ae8849c

Browse files
REF: only fallback to masked op for object dtype (#40728)
1 parent 6bd2640 commit ae8849c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pandas/core/computation/expressions.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,20 @@ def _evaluate_numexpr(op, op_str, a, b):
104104
a_value = a
105105
b_value = b
106106

107-
result = ne.evaluate(
108-
f"a_value {op_str} b_value",
109-
local_dict={"a_value": a_value, "b_value": b_value},
110-
casting="safe",
111-
)
107+
try:
108+
result = ne.evaluate(
109+
f"a_value {op_str} b_value",
110+
local_dict={"a_value": a_value, "b_value": b_value},
111+
casting="safe",
112+
)
113+
except TypeError:
114+
# numexpr raises eg for array ** array with integers
115+
# (https://github.com/pydata/numexpr/issues/379)
116+
pass
117+
118+
if is_reversed:
119+
# reverse order to original for fallback
120+
a, b = b, a
112121

113122
if _TEST_MODE:
114123
_store_test_result(result is not None)

pandas/core/ops/array_ops.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ def _na_arithmetic_op(left, right, op, is_cmp: bool = False):
158158
try:
159159
result = expressions.evaluate(op, left, right)
160160
except TypeError:
161-
if is_cmp:
162-
# numexpr failed on comparison op, e.g. ndarray[float] > datetime
163-
# In this case we do not fall back to the masked op, as that
164-
# will handle complex numbers incorrectly, see GH#32047
161+
if is_object_dtype(left) or is_object_dtype(right) and not is_cmp:
162+
# For object dtype, fallback to a masked operation (only operating
163+
# on the non-missing values)
164+
# Don't do this for comparisons, as that will handle complex numbers
165+
# incorrectly, see GH#32047
166+
result = _masked_arith_op(left, right, op)
167+
else:
165168
raise
166-
result = _masked_arith_op(left, right, op)
167169

168170
if is_cmp and (is_scalar(result) or result is NotImplemented):
169171
# numpy returned a scalar instead of operating element-wise

0 commit comments

Comments
 (0)