Python pandas :使日期索引连续 [英] Python Pandas: Making date index continuous
本文介绍了Python pandas :使日期索引连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下时间序列数据:
I have the following time series data:
EventCount
Date
2015-06-23 1
2015-06-25 1
2015-05-29 1
我需要填写数据,所以索引具有连续的日期,如下所示:
I need to pad the data, so the index has continuous dates, like this:
EventCount
Date
2015-06-23 1
2015-06-24 0
2015-06-25 1
2015-06-26 0
2015-06-27 0
2015-06-28 0
2015-05-29 1
我该怎么办这个?
推荐答案
你可以使用 .reindex()
/ p>
You can use the .reindex()
.
# your data
# ===================================
print(df)
EventCount
Date
2015-06-23 1
2015-06-25 1
2015-06-29 1
# processing
# ==============================
df.reindex(pd.date_range(df.index[0], df.index[-1], freq='D')).fillna(0)
EventCount
2015-06-23 1
2015-06-24 0
2015-06-25 1
2015-06-26 0
2015-06-27 0
2015-06-28 0
2015-06-29 1
这篇关于Python pandas :使日期索引连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文