我在python中有一个数据框,日期为索引,但日期跳过周末和假日。什么是最简单的方法来添加缺少的日期并用前面的行填充列。
例如,2017年1月18日将拥有与2017年3月17日相同的数字,而2017年3月19日的数据与2017年1月18日相同
Index col1 col2 col3 col4
17/3/2017 17975694 4527000 16867630 3640000
20/3/2017 18199194 4670000 17403630 3638000
21/3/2017 17996694 4770500 17656630 3652000
答案 0 :(得分:1)
您可以将resample
与ffill
:
df.resample('D').ffill()
输出:
col1 col2 col3 col4
Index
2017-03-17 17975694 4527000 16867630 3640000
2017-03-18 17975694 4527000 16867630 3640000
2017-03-19 17975694 4527000 16867630 3640000
2017-03-20 18199194 4670000 17403630 3638000
2017-03-21 17996694 4770500 17656630 3652000
答案 1 :(得分:0)
df.reindex(pd.date_range('2017-03-15', '2017-03-25'), method='ffill')