pandas.Series.dt.shift#
- Series.dt.shift(periods=1, freq=None)[source]#
Shift the values by desired number of time frequency increments.
Unlike
Series.shift(), which moves values to different positions, this addsperiods * freqto each value, likeDatetimeIndex.shift().- Parameters:
- periodsint, default 1
Number of periods (or increments) to shift by, can be positive or negative.
- freqpandas.DateOffset, pandas.Timedelta or str, optional
Frequency increment to shift by. If None, the frequency inferred from the values (see
Series.dt.freq) is used, so values in descending order move backward for positiveperiods. Must be None for period values, which are shifted by their own frequency.
- Returns:
- Series
Series with the shifted values, indexed like the original Series.
- Raises:
- NullFrequencyError
If
freqis None,periodsis nonzero, and no frequency can be inferred from the values.- TypeError
If
freqis passed for period values.
See also
DatetimeIndex.shiftShift values of DatetimeIndex.
Series.shiftShift values by desired number of positions.
Examples
>>> ser = pd.Series(pd.date_range("2020-01-01", periods=3, freq="D")) >>> ser.dt.shift(1) 0 2020-01-02 1 2020-01-03 2 2020-01-04 dtype: datetime64[us]
>>> ser.dt.shift(2, freq="h") 0 2020-01-01 02:00:00 1 2020-01-02 02:00:00 2 2020-01-03 02:00:00 dtype: datetime64[us]