gpt4 book ai didi

python - 将不规则时间序列转换为python pandas中的每小时数据

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:49 24 4
gpt4 key购买 nike

我有一个如下所示的数据框:

                read            value
0 2013-01-07 05:00:00 29.0
1 2013-01-08 15:00:00 4034.0
2 2013-01-09 20:00:00 256340.0
3 2013-01-10 20:00:00 343443.0
4 2013-01-11 20:00:00 4642435.0
5 2013-01-12 15:00:00 544296.0
6 2013-01-13 20:00:00 700000.0
7 2013-01-14 20:00:00 782335.0
8 2013-01-15 19:00:00 900000.0
9 2013-01-16 20:00:00 959130.0
10 2013-01-17 19:00:00 1114343.0
11 2013-01-18 20:00:00 1146230.0
12 2013-01-19 20:00:00 1247793.0
13 2013-01-20 20:00:00 1343376.0

我想对其进行转换和规范化,以便显示随时间变化的每小时消耗量。到目前为止,我有以下内容

import numpy as np
import pandas as pd

#caluclates hourly delta
current['hour_delta'] = (current['read'] - current['read'].shift()).fillna(0).astype('timedelta64[h]')


#adds end date and then amount per hours
current['end_date'] = current['read'] + pd.to_timedelta(current['hour_delta'], unit='h')
current['infer_hour'] = current['value'] / current['hour_delta']

然后我创建系列

#create hourly time series
result = pd.Series(0, index=pd.date_range(start=current['read'].min(), end=current['read'].max(), freq='h'))

但是从这里我无法弄清楚如何将小时费率应用于系列。

最佳答案

您可以每小时对 read 列重新采样。然后插值以填充空值。然后取每一行与下一行的差异。

例如,2013-01-07 05:00:002013-01-08 15:00:00 之间有 34 小时。如果我必须在 34 小时内分发 4034,那么每小时的平均值应该是 4034/34118.647059

current.set_index('read').value.cumsum().resample('H').sum().interpolate().diff()

read
2013-01-07 05:00:00 NaN
2013-01-07 06:00:00 118.647059
2013-01-07 07:00:00 118.647059
2013-01-07 08:00:00 118.647059
2013-01-07 09:00:00 118.647059
2013-01-07 10:00:00 118.647059
2013-01-07 11:00:00 118.647059
2013-01-07 12:00:00 118.647059
2013-01-07 13:00:00 118.647059
2013-01-07 14:00:00 118.647059
2013-01-07 15:00:00 118.647059
2013-01-07 16:00:00 118.647059
2013-01-07 17:00:00 118.647059
2013-01-07 18:00:00 118.647059
2013-01-07 19:00:00 118.647059
...

关于python - 将不规则时间序列转换为python pandas中的每小时数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45177744/

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