我有一个很大的数据框 temp_history :
Date Temp
2017-09-14 00:00:00 38.91588479
2017-09-13 00:00:00 38.69563395
2017-09-12 00:00:00 39.25504775
2017-09-11 00:00:00 40.5487144
2017-09-08 00:00:00 41.23372428
2017-09-07 00:00:00 41.5960891
2017-09-06 00:00:00 41.61939397
...
2016-11-30 00:00:00 37.32206175
2016-11-29 00:00:00 36.04661564
2016-11-28 00:00:00 35.70612757
2016-11-25 00:00:00 35.15249571
2016-11-23 00:00:00 36.77833439
...
我今天的数据框 temp_today :
Date Temp
2017-09-15 08:59:02 62.85
现在我使用
将今天的数据附加到历史记录中temp_history = temp_history.append(temp_today)
这会提供组合数据框:
Date Temp
2017-09-15 08:59:02 62.85
2017-09-14 00:00:00 38.91588479
2017-09-13 00:00:00 38.69563395
2017-09-12 00:00:00 39.25504775
2017-09-11 00:00:00 40.5487144
2017-09-08 00:00:00 41.23372428
2017-09-07 00:00:00 41.5960891
2017-09-06 00:00:00 41.61939397
...
现在,当我尝试计算 7天滚动标准时,它不起作用:
temp_history['ST_std'] = temp_history['Temp'].resample('D').ffill().rolling('7D', min_periods=2).std() #rolling 3 mth std
它会计算所有内容但忽略今天滚动标准中的附加值:
Date Temp ST_std
2017-09-15 13:59:02 62.85
2017-09-14 00:00:00 38.91588479 1.163391429
2017-09-13 00:00:00 38.69563395 1.126344307
2017-09-12 00:00:00 39.25504775 0.83068272
2017-09-11 00:00:00 40.5487144 0.831276056
2017-09-08 00:00:00 41.23372428 1.086552698
2017-09-07 00:00:00 41.5960891 0.93493231
2017-09-06 00:00:00 41.61939397 0.897336665
如何让滚动标准包含今天的价值?
答案 0 :(得分:0)
不幸的是,我没有足够的声誉点来回答评论。但它可能是每一行的索引吗?尝试:DataFrame.append中的ignore_index = True
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html