将Pandas数据帧转换为时间序列

gre*_*can 9 pandas

我有一个Pandas DataFrame:

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
Run Code Online (Sandbox Code Playgroud)

我想将其转换为每小时时间序列并插入缺失值(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
Run Code Online (Sandbox Code Playgroud)

如何将数据帧数据转换/映射到Pandas中的时间序列?

Tom*_*ger 11

假设你的'lastrun'有datetime对象:

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
Run Code Online (Sandbox Code Playgroud)

那就是你想要线性插值.在即将发布的.13版本中还有更多选项!