# xarray.DataArray.sel

#### DataArray.sel(indexers=None, method=None, tolerance=None, drop=False, \*\*indexers_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L1730-L1853)

Return a new DataArray whose data is given by selecting index
labels along the specified dimension(s).

In contrast to DataArray.isel, indexers for this method should use
labels instead of integers.

Under the hood, this method is powered by using pandas’s powerful Index
objects. This makes label based indexing essentially just as fast as
using integer indexing.

It also means this method uses pandas’s (well documented) logic for
indexing. This means you can use string shortcuts for datetime indexes
(e.g., ‘2000-01’ to select all values in January 2000). It also means
that slices are treated as inclusive of both the start and stop values,
unlike normal Python indexing.

#### WARNING
Do not try to assign values when using any of the indexing methods
`isel` or `sel`:

```default
da = xr.DataArray([0, 1, 2, 3], dims=["x"])
# DO NOT do this
da.isel(x=[0, 1, 2])[1] = -1
```

Assigning values with the chained indexing using `.sel` or
`.isel` fails silently.

* **Parameters:**
  * **indexers** ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), *optional*) – A dict with keys matching dimensions and values given
    by scalars, slices or arrays of tick labels. For dimensions with
    multi-index, the indexer may also be a dict-like object with keys
    matching index level names.
    If DataArrays are passed as indexers, xarray-style indexing will be
    carried out. See [Indexing and selecting data](../user-guide/indexing.html.md#indexing) for the details.
    One of indexers or indexers_kwargs must be provided.
  * **method** (`{None, "nearest", "pad", "ffill", "backfill", "bfill"}`, *optional*) – Method to use for inexact matches:
    - None (default): only exact matches
    - pad / ffill: propagate last valid index value forward
    - backfill / bfill: propagate next valid index value backward
    - nearest: use nearest valid index value
  * **tolerance** (**optional**) – Maximum distance between original and new labels for inexact
    matches. The values of the index at the matching locations must
    satisfy the equation `abs(index[indexer] - target) <= tolerance`.
  * **drop** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *optional*) – If `drop=True`, drop coordinates variables in indexers instead
    of making them scalar.
  * **\*\*indexers_kwargs** (`{dim: indexer, ...}`, *optional*) – The keyword arguments form of `indexers`.
    One of indexers or indexers_kwargs must be provided.
* **Returns:**
  **obj** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – A new DataArray with the same contents as this DataArray, except the
  data and each dimension is indexed by the appropriate indexers.
  If indexer DataArrays have coordinates that do not conflict with
  this object, then these coordinates will be attached.
  In general, each array’s data will be a view of the array’s data
  in this DataArray, unless vectorized indexing was triggered by using
  an array indexer, in which case the data will be a copy.

#### SEE ALSO
[`Dataset.sel`](xarray.Dataset.sel.html.md#xarray.Dataset.sel)
[`DataArray.isel`](xarray.DataArray.isel.html.md#xarray.DataArray.isel)

[Indexing](https://tutorial.xarray.dev/intermediate/indexing/indexing.html)
: Tutorial material on indexing with Xarray objects

[Indexing and Selecting Data](https://tutorial.xarray.dev/fundamentals/02.1_indexing_Basic.html)
: Tutorial material on basics of indexing

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('5de569e8-3035-481a-8b6e-b3ca595d70c0','cd313e01-6dd8-4317-b799-cd7c652038ad','25a979db-501c-4874-b196-24cc197e2d10','../lite/tree/../notebooks/index.html?path=2e1e00c3_ae16_4ff9_b520_90c1c476e215.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da = xr.DataArray(
...     np.arange(25).reshape(5, 5),
...     coords={"x": np.arange(5), "y": np.arange(5)},
...     dims=("x", "y"),
... )
>>> da
<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]])
Coordinates:
  * x        (x) int64 40B 0 1 2 3 4
  * y        (y) int64 40B 0 1 2 3 4
```

```pycon
>>> tgt_x = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
>>> tgt_y = xr.DataArray(np.linspace(0, 4, num=5), dims="points")
>>> da = da.sel(x=tgt_x, y=tgt_y, method="nearest")
>>> da
<xarray.DataArray (points: 5)> Size: 40B
array([ 0,  6, 12, 18, 24])
Coordinates:
    x        (points) int64 40B 0 1 2 3 4
    y        (points) int64 40B 0 1 2 3 4
Dimensions without coordinates: points
```

<div id="25a979db-501c-4874-b196-24cc197e2d10" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('5de569e8-3035-481a-8b6e-b3ca595d70c0','25a979db-501c-4874-b196-24cc197e2d10')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('5de569e8-3035-481a-8b6e-b3ca595d70c0','25a979db-501c-4874-b196-24cc197e2d10')">Open In Tab</button></div><div id="cd313e01-6dd8-4317-b799-cd7c652038ad" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
