gpt4 book ai didi

pandas - 将Pandas数据框转换为时间序列

转载 作者:行者123 更新时间:2023-12-04 03:44:09 25 4
gpt4 key购买 nike

我有一个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

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

那就是您想要线性插值的原因。即将发布的.13版本中有一个 bunch more options!

关于pandas - 将Pandas数据框转换为时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com