# xarray.DataArray.where

#### DataArray.where(cond, other=<NA>, drop=False)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/common.py#L1138-L1258)

Filter elements from this object according to a condition.

Returns elements from ‘DataArray’, where ‘cond’ is True,
otherwise fill in ‘other’.

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

* **Parameters:**
  * **cond** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [`Dataset`](xarray.Dataset.html.md#xarray.Dataset), or [`callable()`](https://docs.python.org/3/builtins/functions.html#callable)) – Locations at which to preserve this object’s values. dtype must be bool.
    If a callable, the callable is passed this object, and the result is used as
    the value for cond.
  * **other** ([scalar](../user-guide/terminology.html.md#term-scalar), [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [`Dataset`](xarray.Dataset.html.md#xarray.Dataset), or [`callable()`](https://docs.python.org/3/builtins/functions.html#callable), *optional*) – Value to use for locations in this object where `cond` is False.
    By default, these locations are filled with NA. If a callable, it must
    expect this object as its only parameter.
  * **drop** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, coordinate labels that only correspond to False values of
    the condition are dropped from the result.
* **Returns:**
  [`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`Dataset`](xarray.Dataset.html.md#xarray.Dataset) – Same xarray type as caller, with dtype float64.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('63703b41-87e7-40fe-999f-001ea7e8f3fb','96438c69-b67c-495c-885e-861c00d9e631','84aaa8f2-99a6-4d8e-8198-3cec30b0ff59','../lite/tree/../notebooks/index.html?path=c91335fd_87b0_4acf_ab1b_5490db6eb743.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> a = xr.DataArray(np.arange(25).reshape(5, 5), dims=("x", "y"))
>>> a
<xarray.DataArray (x: 5, y: 5)> Size: 200B
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Dimensions without coordinates: x, y
```

```pycon
>>> a.where(a.x + a.y < 4)
<xarray.DataArray (x: 5, y: 5)> Size: 200B
array([[ 0.,  1.,  2.,  3., nan],
       [ 5.,  6.,  7., nan, nan],
       [10., 11., nan, nan, nan],
       [15., nan, nan, nan, nan],
       [nan, nan, nan, nan, nan]])
Dimensions without coordinates: x, y
```

```pycon
>>> a.where(a.x + a.y < 5, -1)
<xarray.DataArray (x: 5, y: 5)> Size: 200B
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8, -1],
       [10, 11, 12, -1, -1],
       [15, 16, -1, -1, -1],
       [20, -1, -1, -1, -1]])
Dimensions without coordinates: x, y
```

```pycon
>>> a.where(a.x + a.y < 4, drop=True)
<xarray.DataArray (x: 4, y: 4)> Size: 128B
array([[ 0.,  1.,  2.,  3.],
       [ 5.,  6.,  7., nan],
       [10., 11., nan, nan],
       [15., nan, nan, nan]])
Dimensions without coordinates: x, y
```

```pycon
>>> a.where(lambda x: x.x + x.y < 4, lambda x: -x)
<xarray.DataArray (x: 5, y: 5)> Size: 200B
array([[  0,   1,   2,   3,  -4],
       [  5,   6,   7,  -8,  -9],
       [ 10,  11, -12, -13, -14],
       [ 15, -16, -17, -18, -19],
       [-20, -21, -22, -23, -24]])
Dimensions without coordinates: x, y
```

<div id="84aaa8f2-99a6-4d8e-8198-3cec30b0ff59" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('63703b41-87e7-40fe-999f-001ea7e8f3fb','84aaa8f2-99a6-4d8e-8198-3cec30b0ff59')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('63703b41-87e7-40fe-999f-001ea7e8f3fb','84aaa8f2-99a6-4d8e-8198-3cec30b0ff59')">Open In Tab</button></div><div id="96438c69-b67c-495c-885e-861c00d9e631" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

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

[`where`](xarray.where.html.md#xarray.where)
: equivalent function
