xarray.Dataset.unstack#

Dataset.unstack(dim=None, *, fill_value=<NA>, sparse=False)[source]#

Unstack existing dimensions corresponding to MultiIndexes into 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. 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. 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 (Dataset) – Dataset with unstacked data.

See also

Dataset.stack

Notes

The order of the new dimension coordinates follows the levels of the MultiIndex. A MultiIndex created by pandas (e.g. when converting a pd.DataFrame or pd.Series with a MultiIndex) usually has sorted levels, so the resulting coordinates will be sorted. A MultiIndex created by Dataset.stack() preserves the original order. Use Dataset.sel() after unstacking to select a specific order if needed.