|
34 | 34 | is_unsigned_integer_dtype,
|
35 | 35 | pandas_dtype,
|
36 | 36 | )
|
37 |
| -from pandas.core.dtypes.generic import ABCIndexClass, ABCPeriodArray, ABCSeries |
| 37 | +from pandas.core.dtypes.generic import ABCSeries |
38 | 38 | from pandas.core.dtypes.inference import is_array_like
|
39 | 39 | from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
|
40 | 40 |
|
@@ -368,16 +368,19 @@ class TimelikeOps:
|
368 | 368 |
|
369 | 369 | def _round(self, freq, mode, ambiguous, nonexistent):
|
370 | 370 | # round the local times
|
371 |
| - values = _ensure_datetimelike_to_i8(self) |
| 371 | + if is_datetime64tz_dtype(self): |
| 372 | + # operate on naive timestamps, then convert back to aware |
| 373 | + naive = self.tz_localize(None) |
| 374 | + result = naive._round(freq, mode, ambiguous, nonexistent) |
| 375 | + aware = result.tz_localize( |
| 376 | + self.tz, ambiguous=ambiguous, nonexistent=nonexistent |
| 377 | + ) |
| 378 | + return aware |
| 379 | + |
| 380 | + values = self.view("i8") |
372 | 381 | result = round_nsint64(values, mode, freq)
|
373 | 382 | result = self._maybe_mask_results(result, fill_value=NaT)
|
374 |
| - |
375 |
| - dtype = self.dtype |
376 |
| - if is_datetime64tz_dtype(self): |
377 |
| - dtype = None |
378 |
| - return self._ensure_localized( |
379 |
| - self._simple_new(result, dtype=dtype), ambiguous, nonexistent |
380 |
| - ) |
| 383 | + return self._simple_new(result, dtype=self.dtype) |
381 | 384 |
|
382 | 385 | @Appender((_round_doc + _round_example).format(op="round"))
|
383 | 386 | def round(self, freq, ambiguous="raise", nonexistent="raise"):
|
@@ -1411,45 +1414,6 @@ def __isub__(self, other): # type: ignore
|
1411 | 1414 | self._freq = result._freq
|
1412 | 1415 | return self
|
1413 | 1416 |
|
1414 |
| - # -------------------------------------------------------------- |
1415 |
| - # Comparison Methods |
1416 |
| - |
1417 |
| - def _ensure_localized( |
1418 |
| - self, arg, ambiguous="raise", nonexistent="raise", from_utc=False |
1419 |
| - ): |
1420 |
| - """ |
1421 |
| - Ensure that we are re-localized. |
1422 |
| -
|
1423 |
| - This is for compat as we can then call this on all datetimelike |
1424 |
| - arrays generally (ignored for Period/Timedelta) |
1425 |
| -
|
1426 |
| - Parameters |
1427 |
| - ---------- |
1428 |
| - arg : Union[DatetimeLikeArray, DatetimeIndexOpsMixin, ndarray] |
1429 |
| - ambiguous : str, bool, or bool-ndarray, default 'raise' |
1430 |
| - nonexistent : str, default 'raise' |
1431 |
| - from_utc : bool, default False |
1432 |
| - If True, localize the i8 ndarray to UTC first before converting to |
1433 |
| - the appropriate tz. If False, localize directly to the tz. |
1434 |
| -
|
1435 |
| - Returns |
1436 |
| - ------- |
1437 |
| - localized array |
1438 |
| - """ |
1439 |
| - |
1440 |
| - # reconvert to local tz |
1441 |
| - tz = getattr(self, "tz", None) |
1442 |
| - if tz is not None: |
1443 |
| - if not isinstance(arg, type(self)): |
1444 |
| - arg = self._simple_new(arg) |
1445 |
| - if from_utc: |
1446 |
| - arg = arg.tz_localize("UTC").tz_convert(self.tz) |
1447 |
| - else: |
1448 |
| - arg = arg.tz_localize( |
1449 |
| - self.tz, ambiguous=ambiguous, nonexistent=nonexistent |
1450 |
| - ) |
1451 |
| - return arg |
1452 |
| - |
1453 | 1417 | # --------------------------------------------------------------
|
1454 | 1418 | # Reductions
|
1455 | 1419 |
|
@@ -1687,38 +1651,3 @@ def maybe_infer_freq(freq):
|
1687 | 1651 | freq_infer = True
|
1688 | 1652 | freq = None
|
1689 | 1653 | return freq, freq_infer
|
1690 |
| - |
1691 |
| - |
1692 |
| -def _ensure_datetimelike_to_i8(other, to_utc=False): |
1693 |
| - """ |
1694 |
| - Helper for coercing an input scalar or array to i8. |
1695 |
| -
|
1696 |
| - Parameters |
1697 |
| - ---------- |
1698 |
| - other : 1d array |
1699 |
| - to_utc : bool, default False |
1700 |
| - If True, convert the values to UTC before extracting the i8 values |
1701 |
| - If False, extract the i8 values directly. |
1702 |
| -
|
1703 |
| - Returns |
1704 |
| - ------- |
1705 |
| - i8 1d array |
1706 |
| - """ |
1707 |
| - from pandas import Index |
1708 |
| - |
1709 |
| - if lib.is_scalar(other) and isna(other): |
1710 |
| - return iNaT |
1711 |
| - elif isinstance(other, (ABCPeriodArray, ABCIndexClass, DatetimeLikeArrayMixin)): |
1712 |
| - # convert tz if needed |
1713 |
| - if getattr(other, "tz", None) is not None: |
1714 |
| - if to_utc: |
1715 |
| - other = other.tz_convert("UTC") |
1716 |
| - else: |
1717 |
| - other = other.tz_localize(None) |
1718 |
| - else: |
1719 |
| - try: |
1720 |
| - return np.array(other, copy=False).view("i8") |
1721 |
| - except TypeError: |
1722 |
| - # period array cannot be coerced to int |
1723 |
| - other = Index(other) |
1724 |
| - return other.asi8 |
0 commit comments