Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas replace daily observations by monthly mean

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? I’d appreciate any help. Many thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading