MCPcopy
hub / github.com/pandas-dev/pandas / _shift_with_freq

Method _shift_with_freq

pandas/core/generic.py:10638–10671  ·  view source on GitHub ↗
(self, periods: int, axis: int, freq)

Source from the content-addressed store, hash-verified

10636
10637 @final
10638 def _shift_with_freq(self, periods: int, axis: int, freq) -> Self:
10639 # see shift.__doc__
10640 # when freq is given, index is shifted, data is not
10641 index = self._get_axis(axis)
10642
10643 if freq == "infer":
10644 freq = getattr(index, "freq", None)
10645
10646 if freq is None:
10647 freq = getattr(index, "inferred_freq", None)
10648
10649 if freq is None:
10650 msg = "Freq was not set in the index hence cannot be inferred"
10651 raise ValueError(msg)
10652
10653 elif isinstance(freq, str):
10654 is_period = isinstance(index, PeriodIndex)
10655 freq = to_offset(freq, is_period=is_period)
10656
10657 if isinstance(index, PeriodIndex):
10658 orig_freq = to_offset(index.freq)
10659 if freq != orig_freq:
10660 assert orig_freq is not None # for mypy
10661 raise ValueError(
10662 f"Given freq {PeriodDtype(freq)._freqstr} "
10663 f"does not match PeriodIndex freq "
10664 f"{PeriodDtype(orig_freq)._freqstr}"
10665 )
10666 new_ax: Index = index.shift(periods)
10667 else:
10668 new_ax = index.shift(periods, freq)
10669
10670 result = self.set_axis(new_ax, axis=axis)
10671 return result.__finalize__(self, method="shift")
10672
10673 @final
10674 def truncate(

Callers 1

shiftMethod · 0.95

Calls 5

_get_axisMethod · 0.95
set_axisMethod · 0.95
PeriodDtypeClass · 0.90
__finalize__Method · 0.80
shiftMethod · 0.45

Tested by

no test coverage detected