Convert daily-to-monthly xarray using customized calendar

I have a 3-D Xarray with a 365-day calendar, having dimensions [3650 x 73 x 144] : (time x lat x lon). I want to convert this 10-year daily dataset into a monthly format.
This can be done using groupby(‘time.month’) for a standard calendar.

However, my target calendar has an unusual month length = [32,30,32,31,31,29,30,29,29,30,30,32].

How can I apply this new month length information to convert the daily dataset into monthly, where each month’s length is based on the above array?

One way to do this is to manually categorize the axis and then group by that. Something like

month_ids = numpy.concat([[0]*32, [1]*30, [2]*32, ...)

years = [1,2,3,4,...]

year_months = numpy.array([])
for Y in years:
    # Year 2, month 12 has a label of '212'
    year_months.append(Y * 100 + month_ids)

ds.coords['year_month'] = ('time', year_months)


I might have some of the syntax wrong, just going by memory here


It works with minor alterations. Thanks @Scott.

1 Like