xarray.DataArray.set_index

DataArray.set_index(self, indexes: Mapping[Hashable, Union[Hashable, Sequence[Hashable]]] = None, append: bool = False, inplace: bool = None, **indexes_kwargs: Union[Hashable, Sequence[Hashable]]) → Union[ForwardRef('DataArray'), NoneType]

Set DataArray (multi-)indexes using one or more existing coordinates.

Parameters
  • indexes ({dim: index, ..}) – Mapping from names matching dimensions and values given by (lists of) the names of existing coordinates or variables to set as new (multi-)index.

  • append (bool, optional) – If True, append the supplied index(es) to the existing index(es). Otherwise replace the existing index(es) (default).

  • **indexes_kwargs (optional) – The keyword arguments form of indexes. One of indexes or indexes_kwargs must be provided.

Returns

obj – Another DataArray, with this data but replaced coordinates.

Return type

DataArray

Examples

>>> arr = xr.DataArray(data=np.ones((2, 3)),
...                    dims=['x', 'y'],
...                    coords={'x':
...                        range(2), 'y':
...                        range(3), 'a': ('x', [3, 4])
...                    })
>>> arr
<xarray.DataArray (x: 2, y: 3)>
array([[1., 1., 1.],
       [1., 1., 1.]])
Coordinates:
  * x        (x) int64 0 1
  * y        (y) int64 0 1 2
    a        (x) int64 3 4
>>> arr.set_index(x='a')
<xarray.DataArray (x: 2, y: 3)>
array([[1., 1., 1.],
       [1., 1., 1.]])
Coordinates:
  * x        (x) int64 3 4
  * y        (y) int64 0 1 2