Set Dataset (multi-)indexes using one or more existing coordinates or variables. This legacy method is limited to pandas (multi-)indexes and 1-dimensional "dimension" coordinates. See :py:meth:`~Dataset.set_xindex` for setting a pandas or a custom Xarray-comp
(
self,
indexes: Mapping[Any, Hashable | Sequence[Hashable]] | None = None,
append: bool = False,
**indexes_kwargs: Hashable | Sequence[Hashable],
)
| 4723 | ) |
| 4724 | |
| 4725 | def set_index( |
| 4726 | self, |
| 4727 | indexes: Mapping[Any, Hashable | Sequence[Hashable]] | None = None, |
| 4728 | append: bool = False, |
| 4729 | **indexes_kwargs: Hashable | Sequence[Hashable], |
| 4730 | ) -> Self: |
| 4731 | """Set Dataset (multi-)indexes using one or more existing coordinates |
| 4732 | or variables. |
| 4733 | |
| 4734 | This legacy method is limited to pandas (multi-)indexes and |
| 4735 | 1-dimensional "dimension" coordinates. See |
| 4736 | :py:meth:`~Dataset.set_xindex` for setting a pandas or a custom |
| 4737 | Xarray-compatible index from one or more arbitrary coordinates. |
| 4738 | |
| 4739 | Parameters |
| 4740 | ---------- |
| 4741 | indexes : {dim: index, ...} |
| 4742 | Mapping from names matching dimensions and values given |
| 4743 | by (lists of) the names of existing coordinates or variables to set |
| 4744 | as new (multi-)index. |
| 4745 | append : bool, default: False |
| 4746 | If True, append the supplied index(es) to the existing index(es). |
| 4747 | Otherwise replace the existing index(es) (default). |
| 4748 | **indexes_kwargs : optional |
| 4749 | The keyword arguments form of ``indexes``. |
| 4750 | One of indexes or indexes_kwargs must be provided. |
| 4751 | |
| 4752 | Returns |
| 4753 | ------- |
| 4754 | obj : Dataset |
| 4755 | Another dataset, with this dataset's data but replaced coordinates. |
| 4756 | |
| 4757 | Examples |
| 4758 | -------- |
| 4759 | >>> arr = xr.DataArray( |
| 4760 | ... data=np.ones((2, 3)), |
| 4761 | ... dims=["x", "y"], |
| 4762 | ... coords={"x": range(2), "y": range(3), "a": ("x", [3, 4])}, |
| 4763 | ... ) |
| 4764 | >>> ds = xr.Dataset({"v": arr}) |
| 4765 | >>> ds |
| 4766 | <xarray.Dataset> Size: 104B |
| 4767 | Dimensions: (x: 2, y: 3) |
| 4768 | Coordinates: |
| 4769 | * x (x) int64 16B 0 1 |
| 4770 | a (x) int64 16B 3 4 |
| 4771 | * y (y) int64 24B 0 1 2 |
| 4772 | Data variables: |
| 4773 | v (x, y) float64 48B 1.0 1.0 1.0 1.0 1.0 1.0 |
| 4774 | >>> ds.set_index(x="a") |
| 4775 | <xarray.Dataset> Size: 88B |
| 4776 | Dimensions: (x: 2, y: 3) |
| 4777 | Coordinates: |
| 4778 | * x (x) int64 16B 3 4 |
| 4779 | * y (y) int64 24B 0 1 2 |
| 4780 | Data variables: |
| 4781 | v (x, y) float64 48B 1.0 1.0 1.0 1.0 1.0 1.0 |
| 4782 |