xarray.Dataset.swap_dims

xarray.Dataset.swap_dims#

Dataset.swap_dims(dims_dict=None, **dims_kwargs)[source]#

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.

  • **dims_kwargs ({existing_dim: new_dim, ...}, optional) – The keyword arguments form of dims_dict. One of dims_dict or dims_kwargs must be provided.

Returns

swapped (Dataset) – Dataset with swapped dimensions.

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> Size: 56B
Dimensions:  (x: 2)
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
    y        (x) int64 16B 0 1
Data variables:
    a        (x) int64 16B 5 7
    b        (x) float64 16B 0.1 2.4
>>> ds.swap_dims({"x": "y"})
<xarray.Dataset> Size: 56B
Dimensions:  (y: 2)
Coordinates:
    x        (y) <U1 8B 'a' 'b'
  * y        (y) int64 16B 0 1
Data variables:
    a        (y) int64 16B 5 7
    b        (y) float64 16B 0.1 2.4
>>> ds.swap_dims({"x": "z"})
<xarray.Dataset> Size: 56B
Dimensions:  (z: 2)
Coordinates:
    x        (z) <U1 8B 'a' 'b'
    y        (z) int64 16B 0 1
Dimensions without coordinates: z
Data variables:
    a        (z) int64 16B 5 7
    b        (z) float64 16B 0.1 2.4