gpt4 book ai didi

pandas - 在特定时间获取 Pandas 时间序列的值(value)

转载 作者:行者123 更新时间:2023-12-04 11:02:08 25 4
gpt4 key购买 nike

假设我有一个时间序列

2019-09-20 00:30:51.234260+00:00    4.63
2019-09-20 00:50:51.292892+00:00 4.40
2019-09-20 01:30:51.273058+00:00 4.54
2019-09-20 01:50:51.270876+00:00 4.44
2019-09-20 02:30:51.267385+00:00 4.55
...
2019-10-30 22:57:35.003066+00:00 1.71
2019-10-30 23:12:34.965801+00:00 1.61
2019-10-30 23:27:34.976495+00:00 1.56
2019-10-30 23:42:34.984976+00:00 1.26
2019-10-30 23:57:34.965543+00:00 1.05

我需要时间值 2019-09-20 00:40:00+00:00 .假设这些值是根据值变化原则记录的,这意味着不需要插值,正确答案是 4.63。我该怎么做?

刚进入 pandas_timeseries['2019-09-20 00:40:00+00:00']当然返回一个 KeyError ......

我正在考虑将时间序列截断为 after='2019-09-20 00:40:00+00:00' 然后获取最后一个值,但这似乎很不雅观。

最佳答案

searchsorted
将返回正在搜索的事物可以插入的位置,同时保持排序。

我假设您的时间序列名称是 s .我减去一个,因为你正在寻找之前的位置。

lookup = pd.Timestamp('2019-09-20 00:40:00+00:00')

s.iloc[s.index.searchsorted(lookup) - 1]

4.63

否则,您可以使用 reindexffill
s.reindex([lookup], method='ffill')

2019-09-20 00:40:00+00:00 4.63
Name: value, dtype: float64

关于pandas - 在特定时间获取 Pandas 时间序列的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735762/

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