简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM