Home > Mobile >  Equivalent to Pandas boolean indexing for xarray?
Equivalent to Pandas boolean indexing for xarray?

Time:01-20

I have an xarray dataset that I would like to index using boolean indexing as I would with Pandas. For example, I have a pandas dataframe:

A B C
5 1 0 
4 1 0
3 5 0
4 3 0
5 2 0

And I want only rows where the value in B is greater than 2, like this:

df = df[df['B'] > 2]

would give

A B C
3 5 0
4 3 0

How could I do the same operation in xarray for an xarray dataset?

CodePudding user response:

https://docs.xarray.dev/en/stable/user-guide/indexing.html#masking-with-where

i think you are looking for this

da.where(df.B > 2,drop=True) #da as a DataArray object

CodePudding user response:

If you’re looking to subset the data along axes based on a Boolean condition on one of the coordinates, you can do this with .sel:

ds.sel(x=(ds.x > 5))

You can similarly select along one or more axes after a reduction operation returns Boolean data indexed by coordinates on the array:

ds.sel(x=(ds.v > 5).any(dim="y"))

To subset the data using a multidimensional mask, use .where. Note however that xarray cannot drop arbitrary points from the middle of an array, you may end up with masked values (nans) still appearing in the result.

  • Related