系列的熊猫滚动意义返回NaN

时间:2018-05-29 13:58:37

标签: python pandas

为什么我会收到Nan的滚动意味着什么?这是此代码的代码和输出。最初我认为我的数据错了,但简单的.mean()可以正常工作。

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()) 

添加了一些数据: .................................................. .................

                       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

1 个答案:

答案 0 :(得分:0)

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