Search code examples
pythonpandasindexingpython-xarrayboolean-indexing

Equivalent to Pandas boolean indexing for xarray?


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?


Solution

  • 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