Search code examples
indexingpython-xarrayobject-slicing

Xarray indexing / slicing to account for seasons


I am using xarray to proccess a pretty long data set. It has been very much fun and I was able to find everything I needed in the online documentation. However now I try to plot data for summer and winter, and it won't work. While I found a lot of documentation on calculating means, I have not found anything on "pure" data. So my code is:

In winter:

da.loc[(dataD['time.month']>10 & dataD['time.month']<3)]

And in summer:

da.loc[(dataD['time.month']>4 & dataD['time.month']<9)]

Which should give me all the data for November to February and May to August. However I always run into the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I also tried slicing, like

dataH.sel(time=slice('1995-06-01', '2005-06-10'))

but I couldn't find a way to skip either the winter or summer months in between.

Btw:I want to avoid looping through the data (90k elements!). I really like the xarray (dask?!?) way to deal with data, so I'd love to keep the code clean and elegant :)

I would appreciate some help. Thanks!


Solution

  • You're very close: you just need an extra set of parentheses when using element-wise comparison and binary operations:

    da.loc[(dataD['time.month']>10) | (dataD['time.month']<3)]  # winter
    da.loc[(dataD['time.month']>4) & (dataD['time.month']<9)]  # summer
    

    (Binary operators like & bind more tightly than comparisons like > in Python.)

    Another option that can be slightly more readable is to use the isin() method (requires xarray v0.10.3 or newer):

    ds.loc[dataD['time.month'].isin([1, 2, 11, 12])]  # winter
    ds.loc[dataD['time.month'].isin([5, 6, 7, 8])]  # summer