Skip to content

REF: call ensure_wrapped_if_datetimelike before the array arithmetic_op #39820

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 all commits
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
2 changes: 2 additions & 0 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
from pandas.core.construction import ensure_wrapped_if_datetimelike
from pandas.core.strings.object_array import ObjectStringArrayMixin


Expand Down Expand Up @@ -395,6 +396,7 @@ def _cmp_method(self, other, op):
other = other._ndarray

pd_op = ops.get_array_op(op)
other = ensure_wrapped_if_datetimelike(other)
with np.errstate(all="ignore"):
result = pd_op(self._ndarray, other)

Expand Down
15 changes: 6 additions & 9 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,17 @@ def arithmetic_op(left: ArrayLike, right: Any, op):
Or a 2-tuple of these in the case of divmod or rdivmod.
"""

# NB: We assume that extract_array has already been called
# on `left` and `right`.
# NB: We assume that extract_array and ensure_wrapped_if_datetimelike
# has already been called on `left` and `right`.
# We need to special-case datetime64/timedelta64 dtypes (e.g. because numpy
# casts integer dtypes to timedelta64 when operating with timedelta64 - GH#22390)
lvalues = ensure_wrapped_if_datetimelike(left)
rvalues = ensure_wrapped_if_datetimelike(right)
rvalues = _maybe_upcast_for_op(rvalues, lvalues.shape)
right = _maybe_upcast_for_op(right, left.shape)

if should_extension_dispatch(lvalues, rvalues) or isinstance(rvalues, Timedelta):
if should_extension_dispatch(left, right) or isinstance(right, Timedelta):
# Timedelta is included because numexpr will fail on it, see GH#31457
res_values = op(lvalues, rvalues)

res_values = op(left, right)
else:
res_values = _na_arithmetic_op(lvalues, rvalues, op)
res_values = _na_arithmetic_op(left, right, op)

return res_values

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import pandas.core.common as com
from pandas.core.construction import (
create_series_with_explicit_dtype,
ensure_wrapped_if_datetimelike,
extract_array,
is_empty_data,
sanitize_array,
Expand Down Expand Up @@ -2872,7 +2873,7 @@ def _binop(self, other: Series, func, level=None, fill_value=None):
if not self.index.equals(other.index):
this, other = self.align(other, level=level, join="outer", copy=False)

this_vals, other_vals = ops.fill_binop(this.values, other.values, fill_value)
this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value)

with np.errstate(all="ignore"):
result = func(this_vals, other_vals)
Expand Down Expand Up @@ -5313,6 +5314,7 @@ def _arith_method(self, other, op):

lvalues = self._values
rvalues = extract_array(other, extract_numpy=True, extract_range=True)
rvalues = ensure_wrapped_if_datetimelike(rvalues)

with np.errstate(all="ignore"):
result = ops.arithmetic_op(lvalues, rvalues, op)
Expand Down