How does you tell pandas to ignore NaN
values when calculating a mean? With min periods, pandas will return NaN
for a number of min_periods
when it encounters a single NaN
.
Example:
pd.DataFrame({ 'x': [np.nan, 0, 1, 2, 3, np.nan, 5, 6, 7, 8, 9]}).rolling(3, min_periods = 3).mean()
Result:
-1 NaN
0 NaN
1 NaN
2 1.0
3 2.0
4 NaN
5 NaN
6 NaN
7 6.0
8 7.0
9 8.0
Desired Result:
-1 NaN
0 NaN
1 NaN
2 1.0
3 2.0
4 2.0
5 3.3
6 4.6
7 6.0
8 7.0
9 8.0