Skip to content
Advertisement

how to extract values based upon month in xarray

I have an array of dimensions (9131,101,191). The first dimension is the days from 1/1/2075 till 31/12/2099. I want to extract all the days which are in the month of July. How can I do this in xarray? I have tried using loops and numpy but not getting the desired result. Ultimately, I want to extract all the arrays which are falling in July and find the mean.

Here is the array, its name is initialize_c3 and its shape is (9131,101,191).

JavaScript

I have tried to groupby according to months. try = arr_c3.groupby(arr_c3.time.dt.month) After this the shape of try is (755,1,1) but want the dimensions of try to be (755,101,191). What I am doing wrong?

Advertisement

Answer

You can use groupby() to calculate the monthly climatology. Then use sel() to select the monthly mean for July:

JavaScript

Another way that avoids calculating the means for the other months is to first filter all days in July:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement