diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index c9db995319cdf..6e4aa1a5efacf 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -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 @@ -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) diff --git a/pandas/core/ops/array_ops.py b/pandas/core/ops/array_ops.py index ba9da8d648597..387d8b463b8b4 100644 --- a/pandas/core/ops/array_ops.py +++ b/pandas/core/ops/array_ops.py @@ -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 diff --git a/pandas/core/series.py b/pandas/core/series.py index e19d521bda3df..85c30096b1001 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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, @@ -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) @@ -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)