alextc
alextc

Reputation: 3515

xarray - group data by specific date ranges

I have an xarray DataArray object called da_ffdi_daily_max.

<xarray.DataArray 'FFDI' (time: 3653, latitude: 106, longitude: 193)>
dask.array<shape=(3653, 106, 193), dtype=float32, chunksize=(1, 106, 193)>
Coordinates:
  * time       (time) datetime64[ns] 2008-01-01 2008-01-02 ... 2017-12-31
  * latitude   (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
  * longitude  (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0

The time coordinate ranges from 2008-01-1 to 2017-12-31. I would like to group the FFDI data in November by three categories, i.e. Nov 01 - 10, Nov 11-20 and Nov 21-30.

How can it be achieved by xarray's groupby function?

Upvotes: 4

Views: 582

Answers (1)

bwc
bwc

Reputation: 1115

Groupby generally tries to classify all of your data into specific groups, whereas it looks like you're trying to pull subsets of data. You could make your desired subsets with general datetime indexing.

a = xr.tutorial.open_dataset('air_temperature')

nov_1_10 = a['air'][(a['time'].dt.day <= 10) & (a['time'].dt.month == 11)]
nov_11_20 = a['air'][(a['time'].dt.day <= 20) & (a['time'].dt.day >= 11) & (a['time'].dt.month == 11)]
nov_21_30 = a['air'][(a['time'].dt.day >= 21) & (a['time'].dt.month == 11)]

Upvotes: 3

Related Questions