简体   繁体   中英

Pandas rolling mean for Series returns NaN

Why do I receive Nan for rolling mean? Here's a code and output for this code. Initially I thought my data's wrong but simple .mean() works OK.

print(df_train.head())
y_hat_avg['mean'] = df_train['pickups'].mean()
print(y_hat_avg.head())
y_hat_avg['moving_avg_forecast'] = df_train['pickups'].rolling(1).mean()
print(y_hat_avg.head()) 

Added some data: ...................................................................

                       pickups
date                        
2014-04-01 00:00:00       12
2014-04-01 01:00:00        5
2014-04-01 02:00:00        2
2014-04-01 03:00:00        4
2014-04-01 04:00:00        3
                     pickups      mean
date                                  
2014-08-01 00:00:00       19  47.25888
2014-08-01 01:00:00       26  47.25888
2014-08-01 02:00:00        9  47.25888
2014-08-01 03:00:00        4  47.25888
2014-08-01 04:00:00       11  47.25888

                     pickups      mean  moving_avg_forecast
date                                                       
2014-08-01 00:00:00       19  47.25888                  NaN
2014-08-01 01:00:00       26  47.25888                  NaN
2014-08-01 02:00:00        9  47.25888                  NaN
2014-08-01 03:00:00        4  47.25888                  NaN
2014-08-01 04:00:00       11  47.25888                  NaN

df_train.index = pd.RangeIndex(len(df_train.index))为我解决了这个问题。

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