【发布时间】:2018-07-18 07:39:19
【问题描述】:
我有一个如下所示的数据框:
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'))
但是,从这里我无法弄清楚如何将小时费率应用于该系列。
【问题讨论】:
-
您需要
pandas.DatetimeIndex().resample(),它完全符合您的要求。 pandas.pydata.org/pandas-docs/stable/generated/… -
在这种情况下我将如何应用它?我觉得我把这件事弄得太复杂了
-
我提供了文档的链接。它有例子。
-
@DYZ,如果我对 OP 的理解正确的话——它并不是那么简单,因为上采样......
-
@MaxU 上重采样有什么问题?