Xarray: selecting only october to march from monthly data using xarray

Created on 14 Jul 2019  路  7Comments  路  Source: pydata/xarray

I have array of monthy data. I want to select monthly values starting from october to march like this
my data = starts from [1950-01,1950-02,1950-03, ...]

this is what I want
selection = [1950-10, 1950-11, 1950-12, 1960-1,1960-2, 1960-3, ...] # notice it continues to 1960

thanks

Most helpful comment

I normally use something like this, which results in the equivalent of @dcherian's suggestion, but is a bit more direct:

data = data.sel(time=data.time.dt.month.isin([1, 2, 3, 10, 11, 12]))

@mada0007 you can then subset in time to truncate the time series further afterwards:

result = data.sel(time=slice('1950-04', '1960-04'))

All 7 comments

You want something like data.where(data.time.dt.month.isin([1,2,3,10,11,12]), drop=True) I think

Hi yes except that selected series start are ordered from 1,2,3,10,11,12, 1, 2, 3, 10,11, 12 in that order.
what I really want is 10,11,12, 1,2,3 10,11,12,1,2,3 the first 1,2,3 in the data must be igored so from 10,11,12 the next 1,2,3 should start from the following year

Thanks

I normally use something like this, which results in the equivalent of @dcherian's suggestion, but is a bit more direct:

data = data.sel(time=data.time.dt.month.isin([1, 2, 3, 10, 11, 12]))

@mada0007 you can then subset in time to truncate the time series further afterwards:

result = data.sel(time=slice('1950-04', '1960-04'))

Nice! I should use sel more

Hi, the problem here is that I have multiple netcd files and the dats vary accros time.

is there any other way to do this ?

If you use data = xr.open_mfdataset(...), the above solution will work

Thanks to all, it works!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

equaeghe picture equaeghe  路  4Comments

jhamman picture jhamman  路  5Comments

phausamann picture phausamann  路  3Comments

tomchor picture tomchor  路  4Comments

benbovy picture benbovy  路  3Comments