【发布时间】:2018-07-17 01:45:18
【问题描述】:
我在一个带有 DateTimeIndex 的 pandas 数据框中有多年的数据,我每 30 分钟测量一次记录。我想获得每月 30 分钟的平均值。换句话说,对于每个月,我希望每个月汇总每 30 分钟(00:00、00:30、...、23:30)的平均值。
示例数据。
from datetime import datetime
import numpy as np
import pandas as pd
datetime_idx = pd.date_range(datetime(2017,1,1), datetime(2018,1,1), freq='30min')
np.random.seed(23)
data = np.random.randint(0, 100, size=len(datetime_idx))
df = pd.DataFrame({'Z': pd.Series(data, datetime_idx)})
df.head()
Z
2017-01-01 00:00:00 83
2017-01-01 00:30:00 40
2017-01-01 01:00:00 73
2017-01-01 01:30:00 54
2017-01-01 02:00:00 31
我尝试过链接重采样,但没有奏效。
df.Z.resample('30min').mean().resample('M').mean()
2017-01-31 49.177419
2017-02-28 50.740327
2017-03-31 49.954973
2017-04-30 48.345833
2017-05-31 49.268145
2017-06-30 48.943056
2017-07-31 49.741263
2017-08-31 49.827285
2017-09-30 50.442361
2017-10-31 48.679435
2017-11-30 49.754861
2017-12-31 50.173387
2018-01-31 94.000000
Freq: M, Name: Z, dtype: float64
【问题讨论】:
标签: python pandas time-series