xarray.where(cond, x, y)

Return elements from x or y depending on cond.

Performs xarray-like broadcasting across input arguments.

  • cond (scalar, array, Variable, DataArray or Dataset with boolean dtype) – When True, return values from x, otherwise returns values from y.

  • y (x,) – Values from which to choose. All dimension coordinates on these objects must be aligned with each other and with cond.


  • In priority order (Dataset, DataArray, Variable or array, whichever)

  • type appears as an input argument.


>>> cond = xr.DataArray([True, False], dims=['x'])
>>> x = xr.DataArray([1, 2], dims=['y'])
>>> xr.where(cond, x, 0)
<xarray.DataArray (x: 2, y: 2)>
array([[1, 2],
       [0, 0]])
Dimensions without coordinates: x, y

See also


corresponding numpy function

Dataset.where(), DataArray.where()