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

Method stack

xarray/core/dataset.py:5280–5328  ·  view source on GitHub ↗

Stack any number of existing dimensions into a single new dimension. New dimensions will be added at the end, and by default the corresponding coordinate variables will be combined into a MultiIndex. Parameters ---------- dim : mapping of hashable t

(
        self,
        dim: Mapping[Any, Sequence[Hashable | EllipsisType]] | None = None,
        create_index: bool | None = True,
        index_cls: type[Index] = PandasMultiIndex,
        **dim_kwargs: Sequence[Hashable | EllipsisType],
    )

Source from the content-addressed store, hash-verified

5278
5279 @partial(deprecate_dims, old_name="dimensions")
5280 def stack(
5281 self,
5282 dim: Mapping[Any, Sequence[Hashable | EllipsisType]] | None = None,
5283 create_index: bool | None = True,
5284 index_cls: type[Index] = PandasMultiIndex,
5285 **dim_kwargs: Sequence[Hashable | EllipsisType],
5286 ) -> Self:
5287 """
5288 Stack any number of existing dimensions into a single new dimension.
5289
5290 New dimensions will be added at the end, and by default the corresponding
5291 coordinate variables will be combined into a MultiIndex.
5292
5293 Parameters
5294 ----------
5295 dim : mapping of hashable to sequence of hashable
5296 Mapping of the form `new_name=(dim1, dim2, ...)`. Names of new
5297 dimensions, and the existing dimensions that they replace. An
5298 ellipsis (`...`) will be replaced by all unlisted dimensions.
5299 Passing a list containing an ellipsis (`stacked_dim=[...]`) will stack over
5300 all dimensions.
5301 create_index : bool or None, default: True
5302
5303 - True: create a multi-index for each of the stacked dimensions.
5304 - False: don't create any index.
5305 - None. create a multi-index only if exactly one single (1-d) coordinate
5306 index is found for every dimension to stack.
5307
5308 index_cls: Index-class, default: PandasMultiIndex
5309 Can be used to pass a custom multi-index type (must be an Xarray index that
5310 implements `.stack()`). By default, a pandas multi-index wrapper is used.
5311 **dim_kwargs
5312 The keyword arguments form of ``dim``.
5313 One of dim or dim_kwargs must be provided.
5314
5315 Returns
5316 -------
5317 stacked : Dataset
5318 Dataset with stacked data.
5319
5320 See Also
5321 --------
5322 Dataset.unstack
5323 """
5324 dim = either_dict_or_kwargs(dim, dim_kwargs, "stack")
5325 result = self
5326 for new_dim, dims in dim.items():
5327 result = result._stack_once(dims, new_dim, index_cls, create_index)
5328 return result
5329
5330 def to_stacked_array(
5331 self,

Callers 15

test_unstack_stringFunction · 0.95
test_unstack_objectFunction · 0.95
test_multiindexFunction · 0.95
test_groupby_drops_nansFunction · 0.95
test_stackMethod · 0.95

Calls 3

either_dict_or_kwargsFunction · 0.90
itemsMethod · 0.80
_stack_onceMethod · 0.45

Tested by 14

test_unstack_stringFunction · 0.76
test_unstack_objectFunction · 0.76
test_multiindexFunction · 0.76
test_groupby_drops_nansFunction · 0.76
test_stackMethod · 0.76