我有一些这样的数据:
2013-10-01 11:11:22, 12
2013-10-01 11:15:22, 12
2013-10-01 15:11:22, 12
2013-10-01 16:11:22, 12
2013-10-01 16:45:22, 12
我使用下面的代码重新取样数据:
df.resample('1H', how=lambda x: len(x))
结果如下:
2013-10-01 11:00:00, 2
2013-10-01 12:00:00, 0
2013-10-01 13:00:00, 0
2013-10-01 14:00:00, 0
2013-10-01 15:00:00, 1
2013-10-01 16:00:00, 2
但我希望整天的时间范围从00:00:00到23:00:00,如何更改重新采样日期范围?
答案 0 :(得分:3)
嗯,您可以在重新采样后reindex
使用一整天的小时段索引。如下所示:
>>> rng = pd.period_range('2013-10-01',periods=24, freq='H').to_timestamp()
>>> df.resample('1H', how=lambda x: len(x)).reindex(index=rng,fill_value=0)
value
2013-10-01 00:00:00 0
2013-10-01 01:00:00 0
2013-10-01 02:00:00 0
2013-10-01 03:00:00 0
2013-10-01 04:00:00 0
2013-10-01 05:00:00 0
2013-10-01 06:00:00 0
2013-10-01 07:00:00 0
2013-10-01 08:00:00 0
2013-10-01 09:00:00 0
2013-10-01 10:00:00 0
2013-10-01 11:00:00 2
2013-10-01 12:00:00 0
2013-10-01 13:00:00 0
2013-10-01 14:00:00 0
2013-10-01 15:00:00 1
2013-10-01 16:00:00 2
2013-10-01 17:00:00 0
2013-10-01 18:00:00 0
2013-10-01 19:00:00 0
2013-10-01 20:00:00 0
2013-10-01 21:00:00 0
2013-10-01 22:00:00 0
2013-10-01 23:00:00 0
答案 1 :(得分:0)
您应首先使用from datetime import datetime
将日期转换为日期时间格式,然后使用pd.Series(datetime.strptime...)
。检查https://docs.python.org/2/library/datetime.html,然后您就可以更轻松地操作数据了。