Compare with prior or subsequent rows
Create lag or lead features for time-series analysis.
Pandas · Module
DataFrame.shift moves values by a chosen number of periods, inserting missing values where needed. With freq, it advances date-like index labels without realigning the data.
DataFrame.shift moves values by a specified number of row periods, leaving newly uncovered positions missing by default. With a date-like index and freq, it instead changes index labels by the offset without realigning the data; iterable periods can produce multiple shifted columns.
DataFrame.shift(periods=1, freq=None, axis=0, fill_value=<no_default>, suffix=None)| Name | Type | Description |
|---|---|---|
periods | int or Sequence | Number of periods to shift; may be positive or negative, or a sequence of integers for multiple shifts. |
freq | DateOffset, tseries.offsets, timedelta, or str, optional | Offset from the tseries module or a time rule; optional. |
axis | {0 or 'index', 1 or 'columns'}, optional | Axis to shift; optional. |
fill_value | object, optional | Scalar used for newly introduced missing values; optional and dtype-dependent when omitted. |
suffix | str, optional | String inserted into generated column names when periods is iterable; optional. |
DataFrame
import pandas as pd
sales = pd.DataFrame({"units": [10, 12, 15]})
sales["previous_units"] = sales["units"].shift(1)
print(sales)Creates a one-row lag column; the first row has no preceding value and therefore contains the default missing value.
Create lag or lead features for time-series analysis.
Generate several lagged versions of columns in one operation.
Move calendar labels while preserving data positions.
Control the values introduced at the edges of a shifted frame.
It shifts values by row periods and inserts missing values where no source row exists. A positive period moves values toward later rows, while a negative period moves them toward earlier rows.
Follow-up: How would you shift calendar labels without moving values? Use a date-like index and pass freq; pandas advances the index by the offset without realigning the data.
periods accepts one integer or a sequence of integers. With a sequence, pandas performs one shift for each integer and returns the resulting columns together.
Follow-up: How can generated columns be distinguished? Pass suffix when periods is iterable; pandas places it after the original column name and before each shift value.
fill_value supplies the scalar used for newly introduced missing positions. If omitted, pandas chooses a dtype-dependent missing value, such as np.nan for numeric data.
Follow-up: What constraint applies to freq? The index must be date or datetime-like, or pandas raises NotImplementedError.
DataFrame.asfreqDataFrame.diffDataFrame.pct_changeSeries.shift