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
(
self,
indexers: Mapping[Any, Any] | None = None,
method: str | None = None,
tolerance=None,
drop: bool = False,
**indexers_kwargs: Any,
)
| 1604 | return self._replace(variable=variable, coords=coords, indexes=indexes) |
| 1605 | |
| 1606 | def sel( |
| 1607 | self, |
| 1608 | indexers: Mapping[Any, Any] | None = None, |
| 1609 | method: str | None = None, |
| 1610 | tolerance=None, |
| 1611 | drop: bool = False, |
| 1612 | **indexers_kwargs: Any, |
| 1613 | ) -> Self: |
| 1614 | """Return a new DataArray whose data is given by selecting index |
| 1615 | labels along the specified dimension(s). |
| 1616 | |
| 1617 | In contrast to `DataArray.isel`, indexers for this method should use |
| 1618 | labels instead of integers. |
| 1619 | |
| 1620 | Under the hood, this method is powered by using pandas's powerful Index |
| 1621 | objects. This makes label based indexing essentially just as fast as |
| 1622 | using integer indexing. |
| 1623 | |
| 1624 | It also means this method uses pandas's (well documented) logic for |
| 1625 | indexing. This means you can use string shortcuts for datetime indexes |
| 1626 | (e.g., '2000-01' to select all values in January 2000). It also means |
| 1627 | that slices are treated as inclusive of both the start and stop values, |
| 1628 | unlike normal Python indexing. |
| 1629 | |
| 1630 | .. warning:: |
| 1631 | |
| 1632 | Do not try to assign values when using any of the indexing methods |
| 1633 | ``isel`` or ``sel``:: |
| 1634 | |
| 1635 | da = xr.DataArray([0, 1, 2, 3], dims=["x"]) |
| 1636 | # DO NOT do this |
| 1637 | da.isel(x=[0, 1, 2])[1] = -1 |
| 1638 | |
| 1639 | Assigning values with the chained indexing using ``.sel`` or |
| 1640 | ``.isel`` fails silently. |
| 1641 | |
| 1642 | Parameters |
| 1643 | ---------- |
| 1644 | indexers : dict, optional |
| 1645 | A dict with keys matching dimensions and values given |
| 1646 | by scalars, slices or arrays of tick labels. For dimensions with |
| 1647 | multi-index, the indexer may also be a dict-like object with keys |
| 1648 | matching index level names. |
| 1649 | If DataArrays are passed as indexers, xarray-style indexing will be |
| 1650 | carried out. See :ref:`indexing` for the details. |
| 1651 | One of indexers or indexers_kwargs must be provided. |
| 1652 | method : {None, "nearest", "pad", "ffill", "backfill", "bfill"}, optional |
| 1653 | Method to use for inexact matches: |
| 1654 | |
| 1655 | - None (default): only exact matches |
| 1656 | - pad / ffill: propagate last valid index value forward |
| 1657 | - backfill / bfill: propagate next valid index value backward |
| 1658 | - nearest: use nearest valid index value |
| 1659 | |
| 1660 | tolerance : optional |
| 1661 | Maximum distance between original and new labels for inexact |
| 1662 | matches. The values of the index at the matching locations must |
| 1663 | satisfy the equation ``abs(index[indexer] - target) <= tolerance``. |