简体   繁体   中英

Pandas DateTimeIndex multiple groupby or resample aggregation

I have a years worth of data in a pandas dataframe with a DateTimeIndex where I have a record measured every 30 minutes. I want to get 30 minute averages per month. Said another way, for each month I want the average value for every 30 minutes (00:00, 00:30, ..., 23:30) aggregated over each month.

Example data.

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

I have tried chaining resample but that has not worked.

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

Not familiar with resample() . So I made a couple changes.

I created the index as a column, and used groupby() to get the mean

df = pd.DataFrame({'Z': pd.Series(data),'ts': pd.Series(datetime_idx)})
df.groupby([df.ts.dt.month,df.ts.dt.hour,df.ts.dt.minute])['Z'].mean()

On my side these two return the same result

df.groupby(df.index.strftime('%Y%m')).mean()
Out[1199]: 
                Z
201701  49.177419
201702  50.740327
201703  49.954973
201704  48.345833
201705  49.268145
201706  48.943056
201707  49.741263
201708  49.827285
201709  50.442361
201710  48.679435
201711  49.754861
201712  50.173387
201801  94.000000


df.Z.resample('M').mean()
Out[1198]: 
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM