Shift index by desired number of time frequency increments. This method is for shifting the values of datetime-like indexes by a specified time increment a given number of times. Parameters ---------- periods : int, default 1 Number of pe
(self, periods: int = 1, freq=None)
| 627 | return data |
| 628 | |
| 629 | def shift(self, periods: int = 1, freq=None) -> Self: |
| 630 | """ |
| 631 | Shift index by desired number of time frequency increments. |
| 632 | This method is for shifting the values of datetime-like indexes |
| 633 | by a specified time increment a given number of times. |
| 634 | |
| 635 | Parameters |
| 636 | ---------- |
| 637 | periods : int, default 1 |
| 638 | Number of periods (or increments) to shift by, |
| 639 | can be positive or negative. |
| 640 | freq : pandas.DateOffset, pandas.Timedelta or string, optional |
| 641 | Frequency increment to shift by. |
| 642 | If None, the index is shifted by its own `freq` attribute. |
| 643 | Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc. |
| 644 | |
| 645 | Returns |
| 646 | ------- |
| 647 | pandas.DatetimeIndex |
| 648 | Shifted index. |
| 649 | |
| 650 | See Also |
| 651 | -------- |
| 652 | Index.shift : Shift values of Index. |
| 653 | PeriodIndex.shift : Shift values of PeriodIndex. |
| 654 | """ |
| 655 | if freq is not None and freq != self.freq: |
| 656 | if isinstance(freq, str): |
| 657 | freq = to_offset(freq) |
| 658 | offset = periods * freq |
| 659 | return self + offset |
| 660 | |
| 661 | if periods == 0 or len(self) == 0: |
| 662 | # GH#14811 empty case |
| 663 | return self.copy() |
| 664 | |
| 665 | if self.freq is None: |
| 666 | raise NullFrequencyError("Cannot shift with no freq") |
| 667 | |
| 668 | start = self[0] + periods * self.freq |
| 669 | end = self[-1] + periods * self.freq |
| 670 | |
| 671 | # Note: in the DatetimeTZ case, _generate_range will infer the |
| 672 | # appropriate timezone from `start` and `end`, so tz does not need |
| 673 | # to be passed explicitly. |
| 674 | result = self._data._generate_range( |
| 675 | start=start, end=end, periods=None, freq=self.freq, unit=self.unit |
| 676 | ) |
| 677 | return type(self)._simple_new(result, name=self.name) |
| 678 | |
| 679 | @cache_readonly |
| 680 | def inferred_freq(self) -> str | None: |
nothing calls this directly
no test coverage detected