MCPcopy Index your code
hub / github.com/pydata/xarray / set_index

Method set_index

xarray/core/dataset.py:4725–4888  ·  view source on GitHub ↗

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],
    )

Source from the content-addressed store, hash-verified

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

Calls 12

either_dict_or_kwargsFunction · 0.90
itemsMethod · 0.80
get_all_coordsMethod · 0.80
to_base_variableMethod · 0.80
joinMethod · 0.45
updateMethod · 0.45
from_variablesMethod · 0.45
create_variablesMethod · 0.45
getMethod · 0.45
_replaceMethod · 0.45