pandas.sum()
will skip NaN by default, so I would expect b.rolling(2).sum()
to also replace the NaN with 0. However, b.rolling(2).sum()
yields NaN instead of 0, where b = pd.Series([np.nan, np.nan])
.
The DataFrame.rolling method takes a min_periods
parameter, which defaults to the size of the window. Until that many non-missing values are accumulated, the rolling calculation will leave the values missing. All your values are missing, so the calculation will never accumulate 2 values.
For this all-NaN case, you could just set the parameter to 0.
pd.Series([np.NaN, np.NaN]).rolling(2, min_periods=0).sum()
# 0 0.0
# 1 0.0
But this will also change the behavior for series where the first two values are present. It will leave the first value in place, not empty to indicate that there weren't 2 values in the rolling sum yet.
pd.Series([1, 2]).rolling(2).sum()
# 0 NaN
# 1 3.0
pd.Series([1, 2]).rolling(2, min_periods=0).sum()
# 0 1.0
# 1 3.0
To avoid that, I would need to know more about your use case.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments