Compute discrete differences between current and prior elements across Series and DataFrames using the Pandas diff() method. Measure period-over-period changes, calculate velocity or acceleration in time series data, and configure custom lag periods.
diff() method calculates the absolute difference between an element and a prior element (specified by the periods parameter). By default, it subtracts the immediately preceding row (`periods=1`). The first row naturally evaluates to `NaN`.import pandas as pd
s = pd.Series([10, 15, 12, 18], name="VAL")
print(s)
assert s.shape == (4,)
Expected output
0 10
1 15
2 12
3 18
Name: VAL, dtype: int64
diff() on the Series to compute row-over-row differences.res = s.diff()
print(res)
assert res.iloc[1] == 5.0 and res.iloc[2] == -3.0
Expected output
0 NaN
1 5.0
2 -3.0
3 6.0
Name: VAL, dtype: float64
periods parameter to calculate differences across multiple rows (e.g., comparing against two periods prior).res_lag = s.diff(periods=2)
print(res_lag)
assert res_lag.iloc[2] == 2.0
Expected output
0 NaN
1 NaN
2 2.0
3 3.0
Name: VAL, dtype: float64
diff() computes row-wise differences (axis=0) by default. Set axis=1 to calculate column-wise differences.df = pd.DataFrame({
"A": [10, 20, 30],
"B": [15, 25, 40]
})
df_diff = df.diff(axis=1)
print(df_diff)
assert df_diff.loc[0, "B"] == 5.0
Expected output
A B
0 NaN 5.0
1 NaN 5.0
2 NaN 10.0
temps = pd.Series([30, 32, 31, 35], name="TEMP"), compute the discrete difference using diff() and verify that the difference at index 3 equals 4.0.diff().temps = pd.Series([30, 32, 31, 35], name="TEMP")
answer = temps.diff()
print(answer)
assert answer.iloc[3] == 4.0
Expected output
0 NaN
1 2.0
2 -1.0
3 4.0
Name: TEMP, dtype: float64
Practice diff() examples interactively directly in your browser:
Reference: Pandas diff() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.