xarray.DataArray.swap_dims

DataArray.swap_dims(dims_dict=None, **dims_kwargs)

Returns a new DataArray with swapped dimensions.

Parameters
  • dims_dict (dict-like) – Dictionary whose keys are current dimension names and whose values are new names.

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

Returns

swapped (DataArray) – DataArray with swapped dimensions.

Examples

>>> arr = xr.DataArray(
...     data=[0, 1],
...     dims="x",
...     coords={"x": ["a", "b"], "y": ("x", [0, 1])},
... )
>>> arr
<xarray.DataArray (x: 2)>
array([0, 1])
Coordinates:
  * x        (x) <U1 'a' 'b'
    y        (x) int64 0 1
>>> arr.swap_dims({"x": "y"})
<xarray.DataArray (y: 2)>
array([0, 1])
Coordinates:
    x        (y) <U1 'a' 'b'
  * y        (y) int64 0 1
>>> arr.swap_dims({"x": "z"})
<xarray.DataArray (z: 2)>
array([0, 1])
Coordinates:
    x        (z) <U1 'a' 'b'
    y        (z) int64 0 1
Dimensions without coordinates: z