🍾 Xarray is now 10 years old! 🎉

xarray.Dataset.isel

Contents

xarray.Dataset.isel#

Dataset.isel(indexers=None, drop=False, missing_dims='raise', **indexers_kwargs)[source]#

Returns a new dataset with each array indexed along the specified dimension(s).

This method selects values from each array using its __getitem__ method, except this method does not require knowing the order of each array’s dimensions.

Parameters:
  • indexers (dict, optional) – A dict with keys matching dimensions and values given by integers, slice objects or arrays. indexer can be a integer, slice, array-like or DataArray. If DataArrays are passed as indexers, xarray-style indexing will be carried out. See Indexing and selecting data for the details. One of indexers or indexers_kwargs must be provided.

  • drop (bool, default: False) – If drop=True, drop coordinates variables indexed by integers instead of making them scalar.

  • missing_dims ({"raise", "warn", "ignore"}, default: "raise") – What to do if dimensions that should be selected from are not present in the Dataset: - “raise”: raise an exception - “warn”: raise a warning, and ignore the missing dimensions - “ignore”: ignore the missing dimensions

  • **indexers_kwargs ({dim: indexer, ...}, optional) – The keyword arguments form of indexers. One of indexers or indexers_kwargs must be provided.

Returns:

obj (Dataset) – A new Dataset with the same contents as this dataset, except each array and 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 dataset, unless vectorized indexing was triggered by using an array indexer, in which case the data will be a copy.

Examples

>>> dataset = xr.Dataset(
...     {
...         "math_scores": (
...             ["student", "test"],
...             [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
...         ),
...         "english_scores": (
...             ["student", "test"],
...             [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
...         ),
...     },
...     coords={
...         "student": ["Alice", "Bob", "Charlie"],
...         "test": ["Test 1", "Test 2", "Test 3"],
...     },
... )

# A specific element from the dataset is selected

>>> dataset.isel(student=1, test=0)
<xarray.Dataset> Size: 68B
Dimensions:         ()
Coordinates:
    student         <U7 28B 'Bob'
    test            <U6 24B 'Test 1'
Data variables:
    math_scores     int64 8B 78
    english_scores  int64 8B 75

# Indexing with a slice using isel

>>> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2))
>>> slice_of_data
<xarray.Dataset> Size: 168B
Dimensions:         (student: 2, test: 2)
Coordinates:
  * student         (student) <U7 56B 'Alice' 'Bob'
  * test            (test) <U6 48B 'Test 1' 'Test 2'
Data variables:
    math_scores     (student, test) int64 32B 90 85 78 80
    english_scores  (student, test) int64 32B 88 90 75 82
>>> index_array = xr.DataArray([0, 2], dims="student")
>>> indexed_data = dataset.isel(student=index_array)
>>> indexed_data
<xarray.Dataset> Size: 224B
Dimensions:         (student: 2, test: 3)
Coordinates:
  * student         (student) <U7 56B 'Alice' 'Charlie'
  * test            (test) <U6 72B 'Test 1' 'Test 2' 'Test 3'
Data variables:
    math_scores     (student, test) int64 48B 90 85 92 95 92 98
    english_scores  (student, test) int64 48B 88 90 92 93 96 91

See also

Dataset.sel DataArray.isel

Indexing

Tutorial material on indexing with Xarray objects

Indexing and Selecting Data

Tutorial material on basics of indexing