Search code examples
pythonpandastime-seriesnan

How to ignore NaN in rolling average calculation in Python


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.


Solution

  • Here is on way adding min_periods

    s=df.Sales.rolling(window=2,min_periods=1).mean()
    s.iloc[0]=np.nan
    s
    Out[1293]: 
    0      NaN
    1    150.0
    2    250.0
    3    250.0
    4    200.0
    Name: Sales, dtype: float64