Skip to content

Commit

Permalink
REF: only fallback to masked op for object dtype (pandas-dev#40728)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored and vladu committed Apr 5, 2021
1 parent 54db432 commit 1c5c42e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 14 additions & 5 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ def _evaluate_numexpr(op, op_str, a, b):
a_value = a
b_value = b

result = ne.evaluate(
f"a_value {op_str} b_value",
local_dict={"a_value": a_value, "b_value": b_value},
casting="safe",
)
try:
result = ne.evaluate(
f"a_value {op_str} b_value",
local_dict={"a_value": a_value, "b_value": b_value},
casting="safe",
)
except TypeError:
# numexpr raises eg for array ** array with integers
# (https://github.com/pydata/numexpr/issues/379)
pass

if is_reversed:
# reverse order to original for fallback
a, b = b, a

if _TEST_MODE:
_store_test_result(result is not None)
Expand Down
12 changes: 7 additions & 5 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ def _na_arithmetic_op(left, right, op, is_cmp: bool = False):
try:
result = expressions.evaluate(op, left, right)
except TypeError:
if is_cmp:
# numexpr failed on comparison op, e.g. ndarray[float] > datetime
# In this case we do not fall back to the masked op, as that
# will handle complex numbers incorrectly, see GH#32047
if is_object_dtype(left) or is_object_dtype(right) and not is_cmp:
# For object dtype, fallback to a masked operation (only operating
# on the non-missing values)
# Don't do this for comparisons, as that will handle complex numbers
# incorrectly, see GH#32047
result = _masked_arith_op(left, right, op)
else:
raise
result = _masked_arith_op(left, right, op)

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

0 comments on commit 1c5c42e

Please sign in to comment.