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

Method unstack

xarray/core/dataset.py:5544–5645  ·  view source on GitHub ↗

Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions. New dimensions will be added at the end. Parameters ---------- dim : str, Iterable of Hashable or None, optional Dimension(s) over which to unstack.

(
        self,
        dim: Dims = None,
        *,
        fill_value: Any = xrdtypes.NA,
        sparse: bool = False,
    )

Source from the content-addressed store, hash-verified

5542 )
5543
5544 def unstack(
5545 self,
5546 dim: Dims = None,
5547 *,
5548 fill_value: Any = xrdtypes.NA,
5549 sparse: bool = False,
5550 ) -> Self:
5551 """
5552 Unstack existing dimensions corresponding to MultiIndexes into
5553 multiple new dimensions.
5554
5555 New dimensions will be added at the end.
5556
5557 Parameters
5558 ----------
5559 dim : str, Iterable of Hashable or None, optional
5560 Dimension(s) over which to unstack. By default unstacks all
5561 MultiIndexes.
5562 fill_value : scalar or dict-like, default: nan
5563 value to be filled. If a dict-like, maps variable names to
5564 fill values. If not provided or if the dict-like does not
5565 contain all variables, the dtype's NA value will be used.
5566 sparse : bool, default: False
5567 use sparse-array if True
5568
5569 Returns
5570 -------
5571 unstacked : Dataset
5572 Dataset with unstacked data.
5573
5574 See Also
5575 --------
5576 Dataset.stack
5577 """
5578
5579 if dim is None:
5580 dims = list(self.dims)
5581 else:
5582 if isinstance(dim, str) or not isinstance(dim, Iterable):
5583 dims = [dim]
5584 else:
5585 dims = list(dim)
5586
5587 missing_dims = set(dims) - set(self.dims)
5588 if missing_dims:
5589 raise ValueError(
5590 f"Dimensions {tuple(missing_dims)} not found in data dimensions {tuple(self.dims)}"
5591 )
5592
5593 # each specified dimension must have exactly one multi-index
5594 stacked_indexes: dict[Any, tuple[Index, dict[Hashable, Variable]]] = {}
5595 for d in dims:
5596 idx, idx_vars = self._get_stack_index(d, multi=True)
5597 if idx is not None:
5598 stacked_indexes[d] = idx, idx_vars
5599
5600 if dim is None:
5601 dims = list(stacked_indexes)

Callers 7

test_unstackMethod · 0.95
test_unstack_errorsMethod · 0.95
test_unstack_sparseMethod · 0.95
_unstack_onceMethod · 0.45
_unstack_full_reindexMethod · 0.45

Calls 6

_get_stack_indexMethod · 0.95
copyMethod · 0.95
array_typeFunction · 0.90
is_duck_dask_arrayFunction · 0.90
_unstack_full_reindexMethod · 0.80
_unstack_onceMethod · 0.45

Tested by 5

test_unstackMethod · 0.76
test_unstack_errorsMethod · 0.76
test_unstack_sparseMethod · 0.76