Time-series changes
Compute period-over-period changes in metrics stored by row.
Pandas · Module
Computes discrete differences between DataFrame elements, comparing with the previous row by default. Use periods to control the shift and axis to choose rows or columns.
DataFrame.diff compares each value with the value a specified number of periods away. By default it compares with the preceding row; setting axis=1 compares across columns, and the output uses float64 values.
DataFrame.diff(periods=1, axis=0)| Name | Type | Description |
|---|---|---|
periods | int, default 1 | Number of periods to shift before calculating the difference; negative values are accepted. |
axis | {0 or 'index', 1 or 'columns'}, default 0 | Direction of the difference: 0 or 'index' for rows, or 1 or 'columns' for columns. |
DataFrame — first differences.
import pandas as pd
df = pd.DataFrame({"sales": [10, 14, 11, 18]})
df["change"] = df["sales"].diff()
print(df)Computes each row's sales change from the preceding row; the first result is missing because no preceding row exists.
Compute period-over-period changes in metrics stored by row.
Measure horizontal changes between related columns.
Create lag-based features for modeling or analysis.
It computes first discrete differences between DataFrame elements. With the defaults, each row is compared with the previous row, so the leading row has no earlier value to compare against.
Follow-up: How would you compare adjacent columns instead? Pass axis=1, which takes differences across columns rather than rows.
periods controls the shift distance used for the comparison and defaults to 1. It also accepts negative values, allowing comparison in the opposite direction.
Follow-up: What does periods=3 do? Each value is compared with the value three rows earlier when using the default axis.
axis accepts 0 or 'index' for differences over rows, and 1 or 'columns' for differences over columns. The default is axis=0.
Follow-up: What output type should callers expect? The method returns a DataFrame containing the first differences.
pandas.DataFrame.shiftpandas.Series.diffpandas.api.typing.DataFrameGroupBy.diff