blueduckyy
blueduckyy

Reputation: 211

Xarray group by multiple runs and time

I have an xarray Dataset that looks like this:

<xarray.Dataset>
Dimensions:          (lat: 100, lon: 130, model: 7, month: 240)
Coordinates:
    lev              float64 1.0
  * month            (month) datetime64[ns] 2040-01-01 2040-02-01 ... 2059-12-01
  * lon              (lon) float32 -99.375 -99.291664 ... -88.708336 -88.625
  * lat              (lat) float32 49.78038 49.696426 ... 41.552795 41.46884
  * model            (model) object 'bcc-csm1-1' 'CCSM4' ... 'MRI-CGCM3'
Data variables:
    month_mean_snow  (month, lat, lon) float32 ...
    month_mean_tmin  (model, month, lat, lon) float32 nan nan ... -6.0107846
    month_mean_tmax  (model, month, lat, lon) float32 nan nan ... -0.30793613
    month_mean_prec  (model, month, lat, lon) float32 nan nan ... 0.5480785
    month_mean_relh  (model, month, lat, lon) float32 nan ... 69.789246
    month_mean_wspd  (model, month, lat, lon) float32 nan 4.9480243 ... 9.585804
    month_mean_rads  (model, month, lat, lon) float32 nan 21.520756 ... 34.00794

The use case is that I have 7 different climate runs for data and then I resampled it to be at a monthly time scale.

Question: How do I groupby both the models and the months at the same time?

Expected Result: Dimensions (lat: 100, lon: 130, month=12)

What I get when I run this:

res.groupby('month.month', 'model').mean('month', 'model')

are dimensions (lat: 100, lon:130, model: 7, month: 12).

Do I need to squeeze or somehow expand model/ month before the groupby and then just do it on month?

Upvotes: 0

Views: 146

Answers (1)

Ryan
Ryan

Reputation: 71

Try this:

res.groupby('month.month').mean(dim=['model','month'])

Upvotes: 2

Related Questions