Pandas Series: shift() function

Series shift() function

The shift() function is used to shift index by desired number of periods with an optional time freq.

When freq is not passed, shift the index without realigning the data. If freq is passed (in this case, the index must be date or datetime, or it will raise a NotImplementedError), the index will be increased using the periods and the freq.


Series.shift(self, periods=1, freq=None, axis=0, fill_value=None)
Pandas Series shift image


Name Description Type/Default Value Required / Optional
periods   Number of periods to shift. Can be positive or negative. int Required
freq Offset to use from the tseries module or time rule (e.g. ‘EOM’). If freq is specified then the index values are shifted but the data is not realigned. That is, use freq if you would like to extend the index when shifting and preserve the original data. DateOffset, tseries.offsets, timedelta, or str,  Optional
axis Shift direction. {0 or ‘index’, 1 or ‘columns’, None}
Default Value: None
fill_value The scalar value to use for newly introduced missing values. the default depends on the dtype of self. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used. object Optional

Returns: Series - Copy of input object, shifted.


Download the above Notebook from here.

Previous: Get the last row(s) without any NaNs in Pandas series
Next: Resample Pandas time-series data