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

Method unstack

xarray/core/dataarray.py:2999–3061  ·  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 = dtypes.NA,
        sparse: bool = False,
    )

Source from the content-addressed store, hash-verified

2997 return self._from_temp_dataset(ds)
2998
2999 def unstack(
3000 self,
3001 dim: Dims = None,
3002 *,
3003 fill_value: Any = dtypes.NA,
3004 sparse: bool = False,
3005 ) -> Self:
3006 """
3007 Unstack existing dimensions corresponding to MultiIndexes into
3008 multiple new dimensions.
3009
3010 New dimensions will be added at the end.
3011
3012 Parameters
3013 ----------
3014 dim : str, Iterable of Hashable or None, optional
3015 Dimension(s) over which to unstack. By default unstacks all
3016 MultiIndexes.
3017 fill_value : scalar or dict-like, default: nan
3018 Value to be filled. If a dict-like, maps variable names to
3019 fill values. Use the data array's name to refer to its
3020 name. If not provided or if the dict-like does not contain
3021 all variables, the dtype's NA value will be used.
3022 sparse : bool, default: False
3023 Use sparse-array if True
3024
3025 Returns
3026 -------
3027 unstacked : DataArray
3028 Array with unstacked data.
3029
3030 Examples
3031 --------
3032 >>> arr = xr.DataArray(
3033 ... np.arange(6).reshape(2, 3),
3034 ... coords=[("x", ["a", "b"]), ("y", [0, 1, 2])],
3035 ... )
3036 >>> arr
3037 <xarray.DataArray (x: 2, y: 3)> Size: 48B
3038 array([[0, 1, 2],
3039 [3, 4, 5]])
3040 Coordinates:
3041 * x (x) <U1 8B 'a' 'b'
3042 * y (y) int64 24B 0 1 2
3043 >>> stacked = arr.stack(z=("x", "y"))
3044 >>> stacked.indexes["z"]
3045 MultiIndex([('a', 0),
3046 ('a', 1),
3047 ('a', 2),
3048 ('b', 0),
3049 ('b', 1),
3050 ('b', 2)],
3051 name='z')
3052 >>> roundtripped = stacked.unstack()
3053 >>> arr.identical(roundtripped)
3054 True
3055
3056 See Also

Callers 1

test_stack_unstackMethod · 0.95

Calls 2

_to_temp_datasetMethod · 0.95
_from_temp_datasetMethod · 0.95

Tested by 1

test_stack_unstackMethod · 0.76