Skip to content

Commit 89c375e

Browse files
REF: call ensure_wrapped_if_datetimelike before the array arithmetic_op
1 parent ccb90d6 commit 89c375e

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
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/internals/ops.py

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from pandas._typing import ArrayLike
1010

11+
from pandas.core.construction import ensure_wrapped_if_datetimelike
12+
1113
if TYPE_CHECKING:
1214
from pandas.core.internals.blocks import Block
1315
from pandas.core.internals.managers import BlockManager
@@ -54,6 +56,8 @@ def operate_blockwise(
5456

5557
res_blks: list[Block] = []
5658
for lvals, rvals, locs, left_ea, right_ea, rblk in _iter_block_pairs(left, right):
59+
lvals = ensure_wrapped_if_datetimelike(lvals)
60+
rvals = ensure_wrapped_if_datetimelike(rvals)
5761
res_values = array_op(lvals, rvals)
5862
if left_ea and not right_ea and hasattr(res_values, "reshape"):
5963
res_values = res_values.reshape(1, -1)

pandas/core/ops/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
algorithms,
3030
roperator,
3131
)
32+
from pandas.core.construction import ensure_wrapped_if_datetimelike
3233
from pandas.core.ops.array_ops import ( # noqa:F401
3334
arithmetic_op,
3435
comp_method_OBJECT_ARRAY,
@@ -140,6 +141,8 @@ def fill_binop(left, right, fill_value):
140141
right = right.copy()
141142
right[right_mask & mask] = fill_value
142143

144+
left = ensure_wrapped_if_datetimelike(left)
145+
right = ensure_wrapped_if_datetimelike(right)
143146
return left, right
144147

145148

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
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,
@@ -5313,6 +5314,8 @@ def _arith_method(self, other, op):
53135314

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

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

0 commit comments

Comments
 (0)