|
16 | 16 | from pandas._libs import (
|
17 | 17 | NaT,
|
18 | 18 | NaTType,
|
19 |
| - Timedelta, |
20 | 19 | iNaT,
|
21 | 20 | lib,
|
22 | 21 | )
|
@@ -367,19 +366,23 @@ def _wrap_results(result, dtype: np.dtype, fill_value=None):
|
367 | 366 | result = np.datetime64("NaT", "ns")
|
368 | 367 | else:
|
369 | 368 | result = np.int64(result).view("datetime64[ns]")
|
| 369 | + # retain original unit |
| 370 | + result = result.astype(dtype, copy=False) |
370 | 371 | else:
|
371 | 372 | # If we have float dtype, taking a view will give the wrong result
|
372 | 373 | result = result.astype(dtype)
|
373 | 374 | elif is_timedelta64_dtype(dtype):
|
374 | 375 | if not isinstance(result, np.ndarray):
|
375 |
| - if result == fill_value: |
376 |
| - result = np.nan |
| 376 | + if result == fill_value or np.isnan(result): |
| 377 | + result = np.timedelta64("NaT").astype(dtype) |
377 | 378 |
|
378 |
| - # raise if we have a timedelta64[ns] which is too large |
379 |
| - if np.fabs(result) > lib.i8max: |
| 379 | + elif np.fabs(result) > lib.i8max: |
| 380 | + # raise if we have a timedelta64[ns] which is too large |
380 | 381 | raise ValueError("overflow in timedelta operation")
|
| 382 | + else: |
| 383 | + # return a timedelta64 with the original unit |
| 384 | + result = np.int64(result).astype(dtype, copy=False) |
381 | 385 |
|
382 |
| - result = Timedelta(result, unit="ns") |
383 | 386 | else:
|
384 | 387 | result = result.astype("m8[ns]").view(dtype)
|
385 | 388 |
|
@@ -641,7 +644,7 @@ def _mask_datetimelike_result(
|
641 | 644 | result[axis_mask] = iNaT # type: ignore[index]
|
642 | 645 | else:
|
643 | 646 | if mask.any():
|
644 |
| - return NaT |
| 647 | + return np.int64(iNaT).view(orig_values.dtype) |
645 | 648 | return result
|
646 | 649 |
|
647 | 650 |
|
|
0 commit comments