将 10 分钟间隔数据汇总为每小时

Aggregate 10 minute interval data to hourly

我有一个以 10 分钟为间隔的温度和湿度读数的 df,例如:

Time                   
1/2/2017 13:00
1/2/2017 13:10
1/2/2017 13:20
1/2/2017 13:30
1/2/2017 13:40
1/2/2017 13:50
1/2/2017 14:00
1/2/2017 14:10
1/2/2017 14:20

我想通过取一个小时内的平均值将 df 转换为每小时:

Time
1/2/2017 13:00
1/2/2017 14:00

我在转换为 datetime 后尝试了 groupby:

times = pd.to_datetime(df.Time)
df.groupby([times.hour, times.minute])

我收到错误:AttributeError: 'Series' object has no attribute 'hour'

我试过了

df.groupby(pd.DatetimeIndex(df['Time']).hour).mean()

但这根据一天中的 24 小时对所有内容进行了分组。

您可以按照以下方式进行:

import pandas as pd
import numpy as np

dates = ['1/2/2017 13:00', '1/2/2017 13:10', '1/2/2017 13:20', 
         '1/2/2017 13:30', '1/2/2017 13:40', '1/2/2017 13:50',
         '1/2/2017 14:00', '1/2/2017 14:10', '1/2/2017 14:20']

numbers = np.random.randint(0, 11, 9)

df = pd.DataFrame(numbers, index=dates)

df

1/2/2017 13:00 5
1/2/2017 13:10 8
1/2/2017 13:20 10
1/2/2017 13:30 1
1/2/2017 13:40 6
1/2/2017 13:50 7
1/2/2017 14:00 10
1/2/2017 14:10 7
1/2/2017 14:20 8

times = pd.to_datetime(df.index)
df.groupby(times.hour).mean()

13 6.166667
14 8.333333

这里的13和14代表小时合计,即13h的平均值,14h的平均值。

这非常有效: df.resample('60T').mean()