Can anyone help me understand the difference between rolling and expanding function from the example given in the pandas docs.
df = DataFrame({'B': [0, 1, 2, np.nan, 4]})
df
B
0 0.0
1 1.0
2 2.0
3 NaN
4 4.0
df.expanding(2).sum()
B
0 NaN # 0 + NaN
1 1.0 # 1 + 0
2 3.0 # 2 + 1
3 3.0 # ??
4 7.0 # ??
df.rolling(2).sum()
B
0 NaN # 0 + NaN
1 1.0 # 1 + 0
2 3.0 # 2 + 1
3 NaN # NaN + 2
4 NaN # 4 + NaN
I give comment to each row to show my understanding of the calculation. Is that true for rolling function? What about expanding? Where are 3 and 7 in 3rd and 4th rows coming from?