Skip to content

Commit 0d92006

Browse files
REF: call ensure_wrapped_if_datetimelike before the array arithmetic_op (#39820)
1 parent fd67546 commit 0d92006

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

pandas/core/arrays/numpy_.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424
from pandas.core.arraylike import OpsMixin
2525
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
26+
from pandas.core.construction import ensure_wrapped_if_datetimelike
2627
from pandas.core.strings.object_array import ObjectStringArrayMixin
2728

2829

@@ -395,6 +396,7 @@ def _cmp_method(self, other, op):
395396
other = other._ndarray
396397

397398
pd_op = ops.get_array_op(op)
399+
other = ensure_wrapped_if_datetimelike(other)
398400
with np.errstate(all="ignore"):
399401
result = pd_op(self._ndarray, other)
400402

pandas/core/ops/array_ops.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,17 @@ def arithmetic_op(left: ArrayLike, right: Any, op):
195195
Or a 2-tuple of these in the case of divmod or rdivmod.
196196
"""
197197

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

206-
if should_extension_dispatch(lvalues, rvalues) or isinstance(rvalues, Timedelta):
204+
if should_extension_dispatch(left, right) or isinstance(right, Timedelta):
207205
# Timedelta is included because numexpr will fail on it, see GH#31457
208-
res_values = op(lvalues, rvalues)
209-
206+
res_values = op(left, right)
210207
else:
211-
res_values = _na_arithmetic_op(lvalues, rvalues, op)
208+
res_values = _na_arithmetic_op(left, right, op)
212209

213210
return res_values
214211

pandas/core/series.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import pandas.core.common as com
101101
from pandas.core.construction import (
102102
create_series_with_explicit_dtype,
103+
ensure_wrapped_if_datetimelike,
103104
extract_array,
104105
is_empty_data,
105106
sanitize_array,
@@ -2872,7 +2873,7 @@ def _binop(self, other: Series, func, level=None, fill_value=None):
28722873
if not self.index.equals(other.index):
28732874
this, other = self.align(other, level=level, join="outer", copy=False)
28742875

2875-
this_vals, other_vals = ops.fill_binop(this.values, other.values, fill_value)
2876+
this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value)
28762877

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

53145315
lvalues = self._values
53155316
rvalues = extract_array(other, extract_numpy=True, extract_range=True)
5317+
rvalues = ensure_wrapped_if_datetimelike(rvalues)
53165318

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

0 commit comments

Comments
 (0)