xarray.Dataset.where

Dataset.where(cond, other=None, drop=False)

Return an object of the same shape with all entries where cond is True and all other entries masked.

This operation follows the normal broadcasting and alignment rules that xarray uses for binary arithmetic.

Parameters:

cond : boolean DataArray or Dataset

other : unimplemented, optional

Unimplemented placeholder for compatibility with future numpy / pandas versions

drop : boolean, optional

Coordinate labels that only correspond to NA values should be dropped

Returns:

same type as caller or if drop=True same type as caller with dimensions

reduced for dim element where mask is True

Examples

>>> import numpy as np
>>> a = xr.DataArray(np.arange(25).reshape(5, 5), dims=('x', 'y'))
>>> a.where((a > 6) & (a < 18))
<xarray.DataArray (x: 5, y: 5)>
array([[ nan,  nan,  nan,  nan,  nan],
       [ nan,  nan,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.,  14.],
       [ 15.,  16.,  17.,  nan,  nan],
       [ nan,  nan,  nan,  nan,  nan]])
Coordinates:
  * y        (y) int64 0 1 2 3 4
  * x        (x) int64 0 1 2 3 4
>>> a.where((a > 6) & (a < 18), drop=True)
<xarray.DataArray (x: 5, y: 5)>
array([[ nan,  nan,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.,  14.],
       [ 15.,  16.,  17.,  nan,  nan],
Coordinates:
  * x        (x) int64 1 2 3
  * y        (y) int64 0 1 2 3 4