xarray.Dataset.isel (original) (raw)

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:

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:

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: