diff --git a/pandas/core/array_algos/take.py b/pandas/core/array_algos/take.py index faac64d10be66..78ab8f399c807 100644 --- a/pandas/core/array_algos/take.py +++ b/pandas/core/array_algos/take.py @@ -342,13 +342,9 @@ def wrapper( if out_dtype is not None: out = out.view(out_dtype) if fill_wrap is not None: - # FIXME: if we get here with dt64/td64 we need to be sure we have - # matching resos - if fill_value.dtype.kind == "m": - fill_value = fill_value.astype("m8[ns]") - else: - fill_value = fill_value.astype("M8[ns]") - fill_value = fill_wrap(fill_value) + # Assumes fill_value resolution matches arr resolution; currently + # safe because the dispatch dicts only have dt64[ns]/td64[ns] entries. + fill_value = fill_value.view("i8") f(arr, indexer, out, fill_value=fill_value, allow_fill=allow_fill) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index f8bf648cb64e1..55c0bcb5beb84 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -354,35 +354,32 @@ def _wrap_results(result, dtype: np.dtype, fill_value=None): if fill_value is None: # GH#24293 fill_value = iNaT + unit = np.datetime_data(dtype)[0] if not isinstance(result, np.ndarray): assert not isna(fill_value), "Expected non-null fill_value" if result == fill_value: result = np.nan if isna(result): - result = np.datetime64("NaT", "ns").astype(dtype) + result = np.datetime64("NaT", unit) # type: ignore[call-overload] else: - result = np.int64(result).view(dtype) - # retain original unit - result = result.astype(dtype, copy=False) + result = np.datetime64(int(result), unit) # type: ignore[call-overload] else: - # If we have float dtype, taking a view will give the wrong result result = result.astype(dtype) elif dtype.kind == "m": + unit = np.datetime_data(dtype)[0] if not isinstance(result, np.ndarray): if result == fill_value or np.isnan(result): - unit = np.datetime_data(dtype)[0] result = np.timedelta64("NaT", unit) # type: ignore[call-overload] elif np.fabs(result) > lib.i8max: # raise if we have a timedelta64[ns] which is too large raise ValueError("overflow in timedelta operation") else: - # return a timedelta64 with the original unit - result = np.int64(result).astype(dtype, copy=False) + result = np.timedelta64(int(result), unit) # type: ignore[call-overload] else: - result = result.astype("m8[ns]").view(dtype) + result = result.astype(dtype) return result @@ -654,11 +651,14 @@ def _mask_datetimelike_result( ) -> np.ndarray | np.datetime64 | np.timedelta64 | NaTType: if isinstance(result, np.ndarray): # we need to apply the mask - result = result.astype("i8").view(orig_values.dtype) + result = result.astype(orig_values.dtype) axis_mask = mask.any(axis=axis) result[axis_mask] = iNaT elif mask.any(): - return np.int64(iNaT).view(orig_values.dtype) + unit = np.datetime_data(orig_values.dtype)[0] + if orig_values.dtype.kind == "M": + return np.datetime64("NaT", unit) # type: ignore[call-overload] + return np.timedelta64("NaT", unit) # type: ignore[call-overload] return result