MCPcopy
hub / github.com/pydata/xarray / reset_index

Method reset_index

xarray/core/dataset.py:4890–5000  ·  view source on GitHub ↗

Reset the specified index(es) or multi-index level(s). This legacy method is specific to pandas (multi-)indexes and 1-dimensional "dimension" coordinates. See the more generic :py:meth:`~Dataset.drop_indexes` and :py:meth:`~Dataset.set_xindex` method to respectively

(
        self,
        dims_or_levels: Hashable | Sequence[Hashable],
        *,
        drop: bool = False,
    )

Source from the content-addressed store, hash-verified

4888 )
4889
4890 def reset_index(
4891 self,
4892 dims_or_levels: Hashable | Sequence[Hashable],
4893 *,
4894 drop: bool = False,
4895 ) -> Self:
4896 """Reset the specified index(es) or multi-index level(s).
4897
4898 This legacy method is specific to pandas (multi-)indexes and
4899 1-dimensional "dimension" coordinates. See the more generic
4900 :py:meth:`~Dataset.drop_indexes` and :py:meth:`~Dataset.set_xindex`
4901 method to respectively drop and set pandas or custom indexes for
4902 arbitrary coordinates.
4903
4904 Parameters
4905 ----------
4906 dims_or_levels : Hashable or Sequence of Hashable
4907 Name(s) of the dimension(s) and/or multi-index level(s) that will
4908 be reset.
4909 drop : bool, default: False
4910 If True, remove the specified indexes and/or multi-index levels
4911 instead of extracting them as new coordinates (default: False).
4912
4913 Returns
4914 -------
4915 obj : Dataset
4916 Another dataset, with this dataset's data but replaced coordinates.
4917
4918 See Also
4919 --------
4920 Dataset.set_index
4921 Dataset.set_xindex
4922 Dataset.drop_indexes
4923 """
4924 if isinstance(dims_or_levels, str) or not isinstance(dims_or_levels, Sequence):
4925 dims_or_levels = [dims_or_levels]
4926
4927 invalid_coords = set(dims_or_levels) - set(self._indexes)
4928 if invalid_coords:
4929 raise ValueError(
4930 f"{tuple(invalid_coords)} are not coordinates with an index"
4931 )
4932
4933 drop_indexes: set[Hashable] = set()
4934 drop_variables: set[Hashable] = set()
4935 seen: set[Index] = set()
4936 new_indexes: dict[Hashable, Index] = {}
4937 new_variables: dict[Hashable, Variable] = {}
4938
4939 def drop_or_convert(var_names):
4940 if drop:
4941 drop_variables.update(var_names)
4942 else:
4943 base_vars = {
4944 k: self._variables[k].to_base_variable() for k in var_names
4945 }
4946 new_variables.update(base_vars)
4947

Calls 7

get_all_coordsMethod · 0.80
keep_levelsMethod · 0.80
itemsMethod · 0.80
addMethod · 0.45
updateMethod · 0.45
create_variablesMethod · 0.45