|
33 | 33 | npt,
|
34 | 34 | )
|
35 | 35 | from pandas.compat.numpy import function as nv
|
| 36 | +from pandas.errors import NullFrequencyError |
36 | 37 | from pandas.util._decorators import (
|
37 | 38 | Appender,
|
38 | 39 | cache_readonly,
|
@@ -353,10 +354,7 @@ def shift(self: _T, periods: int = 1, freq=None) -> _T:
|
353 | 354 | Index.shift : Shift values of Index.
|
354 | 355 | PeriodIndex.shift : Shift values of PeriodIndex.
|
355 | 356 | """
|
356 |
| - arr = self._data.view() |
357 |
| - arr._freq = self.freq |
358 |
| - result = arr._time_shift(periods, freq=freq) |
359 |
| - return type(self)._simple_new(result, name=self.name) |
| 357 | + raise NotImplementedError |
360 | 358 |
|
361 | 359 | # --------------------------------------------------------------------
|
362 | 360 |
|
@@ -400,6 +398,32 @@ def values(self) -> np.ndarray:
|
400 | 398 | # NB: For Datetime64TZ this is lossy
|
401 | 399 | return self._data._ndarray
|
402 | 400 |
|
| 401 | + @doc(DatetimeIndexOpsMixin.shift) |
| 402 | + def shift(self: _TDT, periods: int = 1, freq=None) -> _TDT: |
| 403 | + if freq is not None and freq != self.freq: |
| 404 | + if isinstance(freq, str): |
| 405 | + freq = to_offset(freq) |
| 406 | + offset = periods * freq |
| 407 | + return self + offset |
| 408 | + |
| 409 | + if periods == 0 or len(self) == 0: |
| 410 | + # GH#14811 empty case |
| 411 | + return self.copy() |
| 412 | + |
| 413 | + if self.freq is None: |
| 414 | + raise NullFrequencyError("Cannot shift with no freq") |
| 415 | + |
| 416 | + start = self[0] + periods * self.freq |
| 417 | + end = self[-1] + periods * self.freq |
| 418 | + |
| 419 | + # Note: in the DatetimeTZ case, _generate_range will infer the |
| 420 | + # appropriate timezone from `start` and `end`, so tz does not need |
| 421 | + # to be passed explicitly. |
| 422 | + result = self._data._generate_range( |
| 423 | + start=start, end=end, periods=None, freq=self.freq |
| 424 | + ) |
| 425 | + return type(self)._simple_new(result, name=self.name) |
| 426 | + |
403 | 427 | # --------------------------------------------------------------------
|
404 | 428 | # Set Operation Methods
|
405 | 429 |
|
|
0 commit comments