xarray.Dataset.swap_dims

Dataset.swap_dims(self, dims_dict: Mapping[Hashable, Hashable], inplace: bool = None) → 'Dataset'

Returns a new object with swapped dimensions.

Parameters

dims_dict (dict-like) – Dictionary whose keys are current dimension names and whose values are new names. Each value must already be a variable in the dataset.

Returns

swapped – Dataset with swapped dimensions.

Return type

Dataset

Examples

>>> ds = xr.Dataset(data_vars={"a": ("x", [5, 7]), "b": ("x", [0.1, 2.4])},
                    coords={"x": ["a", "b"], "y": ("x", [0, 1])})
>>> ds
<xarray.Dataset>
Dimensions:  (x: 2)
Coordinates:
  * x        (x) <U1 'a' 'b'
    y        (x) int64 0 1
Data variables:
    a        (x) int64 5 7
    b        (x) float64 0.1 2.4
>>> ds.swap_dims({"x": "y"})
<xarray.Dataset>
Dimensions:  (y: 2)
Coordinates:
    x        (y) <U1 'a' 'b'
  * y        (y) int64 0 1
Data variables:
    a        (y) int64 5 7
    b        (y) float64 0.1 2.4