# xarray.where

### xarray.where(cond, x, y, keep_attrs=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/computation.py#L673-L823)

Return elements from x or y depending on cond.

Performs xarray-like broadcasting across input arguments.

All dimension coordinates on x and y  must be aligned with each
other and with cond.

* **Parameters:**
  * **cond** ([scalar](../user-guide/terminology.html.md#term-scalar), [array](https://numpy.org/doc/stable/glossary.html#term-array), [`Variable`](xarray.Variable.html.md#xarray.Variable), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – When True, return values from x, otherwise returns values from y.
  * **x** ([scalar](../user-guide/terminology.html.md#term-scalar), [array](https://numpy.org/doc/stable/glossary.html#term-array), [`Variable`](xarray.Variable.html.md#xarray.Variable), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – values to choose from where cond is True
  * **y** ([scalar](../user-guide/terminology.html.md#term-scalar), [array](https://numpy.org/doc/stable/glossary.html#term-array), [`Variable`](xarray.Variable.html.md#xarray.Variable), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – values to choose from where cond is False
  * **keep_attrs** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or `{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}` or [`callable()`](https://docs.python.org/3/builtins/functions.html#callable), *optional*) –
    - ‘override’ or True (default): skip comparing and copy attrs from x to the result.
    - ‘drop’ or False: empty attrs on returned xarray object.
    - ‘identical’: all attrs must be the same on every object.
    - ‘no_conflicts’: attrs from all objects are combined, any that have the same name must also have the same value.
    - ‘drop_conflicts’: attrs from all objects are combined, any that have the same name but different values are dropped.
* **Returns:**
  [`Dataset`](xarray.Dataset.html.md#xarray.Dataset), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [`Variable`](xarray.Variable.html.md#xarray.Variable) or [array](https://numpy.org/doc/stable/glossary.html#term-array) – In priority order: Dataset, DataArray, Variable or array, whichever
  type appears as an input argument.

#### SEE ALSO
[`numpy.where`](https://numpy.org/doc/stable/reference/generated/numpy.where.html#numpy.where)
: corresponding numpy function

[`Dataset.where`](xarray.Dataset.where.html.md#xarray.Dataset.where), [`DataArray.where`](xarray.DataArray.where.html.md#xarray.DataArray.where)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('99120e4b-d85e-4887-84de-ee4f046dcffb','f2c76afc-389c-4b63-9fa3-5e7522ed5eda','29c64fde-2059-40b9-8108-8c02195a3dcc','../lite/tree/../notebooks/index.html?path=e74d35fb_fb53_473f_8b86_49f69f32c020.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> x = xr.DataArray(
...     0.1 * np.arange(10),
...     dims=["lat"],
...     coords={"lat": np.arange(10)},
...     name="sst",
...     attrs={"standard_name": "sea_surface_temperature"},
... )
>>> x
<xarray.DataArray 'sst' (lat: 10)> Size: 80B
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
Coordinates:
  * lat      (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
Attributes:
    standard_name:  sea_surface_temperature
```

```pycon
>>> xr.where(x < 0.5, x, x * 100)
<xarray.DataArray 'sst' (lat: 10)> Size: 80B
array([ 0. ,  0.1,  0.2,  0.3,  0.4, 50. , 60. , 70. , 80. , 90. ])
Coordinates:
  * lat      (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
Attributes:
    standard_name:  sea_surface_temperature
```

If x is a scalar then by default there are no attrs on the result

```pycon
>>> xr.where(x < 0.5, 1, 0)
<xarray.DataArray 'sst' (lat: 10)> Size: 80B
array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
Coordinates:
  * lat      (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
```

If x is a scalar (and therefore has no attrs), preserve the
attrs on cond by using keep_attrs=”drop_conflicts”

```pycon
>>> xr.where(x < 0.5, 1, 0, keep_attrs="drop_conflicts")
<xarray.DataArray 'sst' (lat: 10)> Size: 80B
array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
Coordinates:
  * lat      (lat) int64 80B 0 1 2 3 4 5 6 7 8 9
Attributes:
    standard_name:  sea_surface_temperature
```

```pycon
>>> y = xr.DataArray(
...     0.1 * np.arange(9).reshape(3, 3),
...     dims=["lat", "lon"],
...     coords={"lat": np.arange(3), "lon": 10 + np.arange(3)},
...     name="sst",
... )
>>> y
<xarray.DataArray 'sst' (lat: 3, lon: 3)> Size: 72B
array([[0. , 0.1, 0.2],
       [0.3, 0.4, 0.5],
       [0.6, 0.7, 0.8]])
Coordinates:
  * lat      (lat) int64 24B 0 1 2
  * lon      (lon) int64 24B 10 11 12
```

```pycon
>>> xr.where(y.lat < 1, y, -1)
<xarray.DataArray 'lat' (lat: 3, lon: 3)> Size: 72B
array([[ 0. ,  0.1,  0.2],
       [-1. , -1. , -1. ],
       [-1. , -1. , -1. ]])
Coordinates:
  * lat      (lat) int64 24B 0 1 2
  * lon      (lon) int64 24B 10 11 12
```

```pycon
>>> 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)> Size: 32B
array([[1, 2],
       [0, 0]])
Dimensions without coordinates: x, y
```

<div id="29c64fde-2059-40b9-8108-8c02195a3dcc" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('99120e4b-d85e-4887-84de-ee4f046dcffb','29c64fde-2059-40b9-8108-8c02195a3dcc')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('99120e4b-d85e-4887-84de-ee4f046dcffb','29c64fde-2059-40b9-8108-8c02195a3dcc')">Open In Tab</button></div><div id="f2c76afc-389c-4b63-9fa3-5e7522ed5eda" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
