xarray.DataArray.stack¶
-
DataArray.
stack
(self, dimensions: Mapping[Hashable, Sequence[Hashable]] = None, **dimensions_kwargs: Sequence[Hashable]) → 'DataArray'¶ Stack any number of existing dimensions into a single new dimension.
New dimensions will be added at the end, and the corresponding coordinate variables will be combined into a MultiIndex.
- Parameters
dimensions (Mapping of the form new_name=(dim1, dim2, ..)) – Names of new dimensions, and the existing dimensions that they replace.
**dimensions_kwargs – The keyword arguments form of
dimensions
. One of dimensions or dimensions_kwargs must be provided.
- Returns
stacked – DataArray with stacked data.
- Return type
Examples
>>> arr = 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) |S1 'a' 'b' * y (y) int64 0 1 2 >>> stacked = arr.stack(z=('x', 'y')) >>> stacked.indexes['z'] MultiIndex(levels=[['a', 'b'], [0, 1, 2]], codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], names=['x', 'y'])
See also