unstack
- UnitsAwareDataArray.unstack(dim: Optional[Union[Hashable, Sequence[Hashable]]] = None, fill_value: Any = <NA>, sparse: bool = False) xarray.core.dataarray.DataArray
Unstack existing dimensions corresponding to MultiIndexes into multiple new dimensions.
New dimensions will be added at the end.
- Parameters
dim (hashable or sequence of hashable, optional) – Dimension(s) over which to unstack. By default unstacks all MultiIndexes.
fill_value (scalar or dict-like, default: nan) – value to be filled. If a dict-like, maps variable names to fill values. Use the data array’s name to refer to its name. If not provided or if the dict-like does not contain all variables, the dtype’s NA value will be used.
sparse (bool, default: False) – use sparse-array if True
- Returns
unstacked – Array with unstacked data.
- Return type
DataArray
Examples
>>> arr = xr.DataArray( ... np.arange(6).reshape(2, 3), ... coords=[("x", ["a", "b"]), ("y", [0, 1, 2])], ... ) >>> arr <xarray.DataArray (x: 2, y: 3)> array([[0, 1, 2], [3, 4, 5]]) Coordinates: * x (x) <U1 'a' 'b' * y (y) int64 0 1 2 >>> stacked = arr.stack(z=("x", "y")) >>> stacked.indexes["z"] MultiIndex([('a', 0), ('a', 1), ('a', 2), ('b', 0), ('b', 1), ('b', 2)], names=['x', 'y']) >>> roundtripped = stacked.unstack() >>> arr.identical(roundtripped) True
See also
DataArray.stack