datatree.DataTree.isel#
- DataTree.isel(indexers: Mapping[Any, Any] | None = None, drop: bool = False, missing_dims: ErrorOptionsWithWarn = 'raise', **indexers_kwargs: Any) Self [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
) – Ifdrop=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 ofindexers
. 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> Dimensions: () Coordinates: student <U7 'Bob' test <U6 'Test 1' Data variables: math_scores int64 78 english_scores int64 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> Dimensions: (student: 2, test: 2) Coordinates: * student (student) <U7 'Alice' 'Bob' * test (test) <U6 'Test 1' 'Test 2' Data variables: math_scores (student, test) int64 90 85 78 80 english_scores (student, test) int64 88 90 75 82
>>> index_array = xr.DataArray([0, 2], dims="student") >>> indexed_data = dataset.isel(student=index_array) >>> indexed_data <xarray.Dataset> Dimensions: (student: 2, test: 3) Coordinates: * student (student) <U7 'Alice' 'Charlie' * test (test) <U6 'Test 1' 'Test 2' 'Test 3' Data variables: math_scores (student, test) int64 90 85 92 95 92 98 english_scores (student, test) int64 88 90 92 93 96 91
See also
Dataset.sel DataArray.isel
- xarray-tutorial:intermediate/indexing/indexing
Tutorial material on indexing with Xarray objects
- xarray-tutorial:fundamentals/02.1_indexing_Basic
Tutorial material on basics of indexing