2

I work with monthly climate data (e.g. monthly mean temperature or precipitation) where I am often interested in taking several-month means e.g. December-March or May-September. To do this, I'm attempting to aggregrate monthly time series data using offsets in pandas (version 1.3.5) following the documentation.

For example, I have a monthly time series:

import pandas as pd
index = pd.date_range(start="2000-01-31", end="2000-12-31", freq="M")
data = pd.Series(range(12), index=index)

Taking a 4-month mean:

data_4M = data.resample("4M").mean()
>>> data_4M
2000-01-31     0.0
2000-05-31     2.5
2000-09-30     6.5
2001-01-31    10.0
Freq: 4M, dtype: float64

Attempting a 4-month mean with a 2-month offset produces a warning with the same results as the no-offset example above:

data_4M_offset = data.resample("4M", offset="2M").mean()
c:\program files\python39\lib\site-packages\pandas\core\resample.py:1381: FutureWarning: Units 'M', 'Y' and 'y' do not represent unambiguous timedelta values and will be removed in a future version
  tg = TimeGrouper(**kwds)
>>> data_4M_offset 
2000-01-31     0.0
2000-05-31     2.5
2000-09-30     6.5
2001-01-31    10.0
Freq: 4M, dtype: float64

Does this mean that the monthly offset functionality has already been removed? Is there another way that I can take multi-month averages with offsets?

rpanai
  • 12,515
  • 2
  • 42
  • 64
  • I'm not sure if this is related to my main question: I am also still confused why the first value of data_4M is the average of only 1 month of data. I see this was discussed [here](https://stackoverflow.com/questions/53700107/pandas-time-series-resample-binning-seems-off) but I have trouble understanding this behavior. – Andrew Ronald Friedman Jan 06 '22 at 21:42
  • I changed the definition of index. Here or you import datetime or you can just pass dates as strings. – rpanai Jan 06 '22 at 22:17
  • thanks @rpanai -- I see where I missed the import statement – Andrew Ronald Friedman Jan 06 '22 at 22:53

0 Answers0