# xarray.DataArray.stack

#### DataArray.stack(dim=None, create_index=True, index_cls=<class 'xarray.core.indexes.PandasMultiIndex'>, \*\*dim_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L3045-L3116)

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:**
  * **dim** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of `Hashable` to [sequence](https://docs.python.org/3/glossary.html#term-sequence) of `Hashable`) – Mapping of the form new_name=(dim1, dim2, …).
    Names of new dimensions, and the existing dimensions that they
    replace. An ellipsis (…) will be replaced by all unlisted dimensions.
    Passing a list containing an ellipsis (stacked_dim=[…]) will stack over
    all dimensions.
  * **create_index** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If True, create a multi-index for each of the stacked dimensions.
    If False, don’t create any index.
    If None, create a multi-index only if exactly one single (1-d) coordinate
    index is found for every dimension to stack.
  * **index_cls** (`class`, *optional*) – Can be used to pass a custom multi-index type. Must be an Xarray index that
    implements .stack(). By default, a pandas multi-index wrapper is used.
  * **\*\*dim_kwargs** – The keyword arguments form of `dim`.
    One of dim or dim_kwargs must be provided.
* **Returns:**
  **stacked** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – DataArray with stacked data.

#### SEE ALSO
[`DataArray.unstack`](xarray.DataArray.unstack.html.md#xarray.DataArray.unstack)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('c026e2b7-9a7c-464e-85d7-f766f3c71e00','e446ec28-1370-4fa2-bf9d-289b3664f96f','082f6911-1b0f-4467-9784-c9b7e4ae683e','../lite/tree/../notebooks/index.html?path=0d834599_5025_458a_94b0_dbee1165fc56.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> arr = xr.DataArray(
...     np.arange(6).reshape(2, 3),
...     coords=[("x", ["a", "b"]), ("y", [0, 1, 2])],
... )
>>> arr
<xarray.DataArray (x: 2, y: 3)> Size: 48B
array([[0, 1, 2],
       [3, 4, 5]])
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) int64 24B 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)],
           name='z')
```

<div id="082f6911-1b0f-4467-9784-c9b7e4ae683e" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('c026e2b7-9a7c-464e-85d7-f766f3c71e00','082f6911-1b0f-4467-9784-c9b7e4ae683e')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('c026e2b7-9a7c-464e-85d7-f766f3c71e00','082f6911-1b0f-4467-9784-c9b7e4ae683e')">Open In Tab</button></div><div id="e446ec28-1370-4fa2-bf9d-289b3664f96f" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
