For a time series sales forecasting task I want to create a feature that represents the average sales over the last 3 days. I have a problem when I want to predict the sales for days in the future, since these data points do not have sales data (NaN values). Pandas offers rolling_mean(), but that function results in a NaN ouput when any data point in the window is NaN.
My data:
Date Sales
02-01-2013 100.0
03-01-2013 200.0
04-01-2013 300.0
05-01-2013 200.0
06-01-2013 NaN
Result after using pd.rolling_mean() with window of 2:
Date Rolling_Sales
02-01-2013 NaN
03-01-2013 150.0
04-01-2013 250.0
05-01-2013 250.0
06-01-2013 NaN
Desired result:
Date Rolling_Sales
02-01-2013 NaN
03-01-2013 150.0
04-01-2013 250.0
05-01-2013 250.0
06-01-2013 200.0
So in case the a NaN is included, I want to ignore it and take the average of all the other data points in the window.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…