I have the following code:
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({'clicks': [i for i in range(1000)]})
# Calculate the max of the next 140 rows for each row
roll = df['clicks'].rolling(window=141, min_periods=1).max()
print(roll)
Somehow it doesnt return the expected output:
0 141.0
1 142.0
2 143.0
3 144.0
4 145.0
But it outputs:
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
What do I do wrong?
You need to shift it by 140 rows forward so that the window includes the next 140 rows, and not just the first, second, third etc leading up to 999.
# Calculate the rolling max of the next 140 rows for each row
roll = df['clicks'].shift(-140).rolling(window=141, min_periods=1).max()