xarray.Dataset.to_stacked_array

Dataset.to_stacked_array(self, new_dim: Hashable, sample_dims: Sequence[Hashable], variable_dim: str = 'variable', name: Hashable = None) → 'DataArray'

Combine variables of differing dimensionality into a DataArray without broadcasting.

This method is similar to Dataset.to_array but does not broadcast the variables.

Parameters
  • new_dim (Hashable) – Name of the new stacked coordinate

  • sample_dims (Sequence[Hashable]) – Dimensions that will not be stacked. Each array in the dataset must share these dimensions. For machine learning applications, these define the dimensions over which samples are drawn.

  • variable_dim (str, optional) – Name of the level in the stacked coordinate which corresponds to the variables.

  • name (str, optional) – Name of the new data array.

Returns

stacked – DataArray with the specified dimensions and data variables stacked together. The stacked coordinate is named new_dim and represented by a MultiIndex object with a level containing the data variable names. The name of this level is controlled using the variable_dim argument.

Return type

DataArray

Examples

>>> data = xr.Dataset(
...     data_vars={
...         "a": (("x", "y"), [[0, 1, 2], [3, 4, 5]]),
...         "b": ("x", [6, 7]),
...     },
...     coords={"y": ["u", "v", "w"]},
... )
>>> data
<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
* y        (y) <U1 'u' 'v' 'w'
Dimensions without coordinates: x
Data variables:
    a        (x, y) int64 0 1 2 3 4 5
    b        (x) int64 6 7
>>> data.to_stacked_array("z", sample_dims=["x"])
<xarray.DataArray (x: 2, z: 4)>
array([[0, 1, 2, 6],
    [3, 4, 5, 7]])
Coordinates:
* z         (z) MultiIndex
- variable  (z) object 'a' 'a' 'a' 'b'
- y         (z) object 'u' 'v' 'w' nan
Dimensions without coordinates: x