我有一个熊猫数据框:
Out[57]:
lastrun rate
0 2013-11-04 12:15:02 0
1 2013-11-04 13:14:50 4
2 2013-11-04 14:14:48 10
3 2013-11-04 16:14:59 16
我想将其转换为每小时时间序列并插入缺失值(15:00),以便我最终得到:
2013-11-04 12:00:00 0
2013-11-04 13:00:00 4
2013-11-04 14:00:00 10
2013-11-04 15:00:00 13
2013-11-04 16:00:00 16
如何将数据帧数据转换/映射到 Pandas 中的时间序列?
假设您的“lastrun”有日期时间对象:
In [22]: s = df.set_index('lastrun').resample('H')['rate']
In [23]: s
Out[23]:
lastrun
2013-11-04 12:00:00 0
2013-11-04 13:00:00 4
2013-11-04 14:00:00 10
2013-11-04 15:00:00 NaN
2013-11-04 16:00:00 16
Freq: H, dtype: float64
In [24]: s.interpolate()
Out[24]:
lastrun
2013-11-04 12:00:00 0
2013-11-04 13:00:00 4
2013-11-04 14:00:00 10
2013-11-04 15:00:00 13
2013-11-04 16:00:00 16
Freq: H, dtype: int64
那就是如果你想要线性插值。有一个更多选择 http://pandas.pydata.org/pandas-docs/dev/missing_data.html#interpolation在即将发布的 .13 版本中!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)