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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19914944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:19:28  来源:igfitidea点击:

Convert Pandas dataframe to time series

pandas

提问by greenafrican

I have a Pandas DataFrame:

我有一个Pandas数据帧:

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

I would like to convert that into an hourly time series and interpolate missing values (15:00) so that I end up with:

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

How do I convert / map the dataframe data to a time series in Pandas?

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

回答by TomAugspurger

Assuming your 'lastrun' has datetime objects:

假设你的“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

That's if you want linear interpolation. There's a bunch more optionsin the upcoming .13 release!

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