Home > Software design >  Pandas replace daily observations by monthly mean
Pandas replace daily observations by monthly mean

Time:07-07

Suppose, I have a pandas Series with daily observations:

pd_series = pd.Series(np.random.rand(26281), index = pd.date_range('2022-01-01', '2024-12-31', freq = 'H'))
pd_series
2022-01-01 00:00:00    0.933746
2022-01-01 01:00:00    0.588907
2022-01-01 02:00:00    0.229040
2022-01-01 03:00:00    0.557752
2022-01-01 04:00:00    0.798649
  
2024-12-30 20:00:00    0.314143
2024-12-30 21:00:00    0.670485
2024-12-30 22:00:00    0.300531
2024-12-30 23:00:00    0.075403
2024-12-31 00:00:00    0.716685

What I want is to replace every observation by the monthly average. I know that the average can be calculated as

pd_series.resample('MS').mean()

But how do I put the observations to the respective observations?

CodePudding user response:

Use Resampler.transform:

print (pd_series.resample('MS').transform('mean'))
2022-01-01 00:00:00    0.495015
2022-01-01 01:00:00    0.495015
2022-01-01 02:00:00    0.495015
2022-01-01 03:00:00    0.495015
2022-01-01 04:00:00    0.495015
  
2024-12-30 20:00:00    0.508646
2024-12-30 21:00:00    0.508646
2024-12-30 22:00:00    0.508646
2024-12-30 23:00:00    0.508646
2024-12-31 00:00:00    0.508646
Freq: H, Length: 26281, dtype: float64
  • Related