Skip to content

REF: only fallback to masked op for object dtype #40728

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
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ 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:
Copy link
Member Author

Choose a reason for hiding this comment

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

Catching the error here, instead of higher op in _na_arithmetic_op, ensures that we try again with the plain standard op instead of directly try again with _masked_arith_op

# numexpr raises eg for array ** array with integers
# (https://github.com/pydata/numexpr/issues/379)
pass

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