swap_dims

UnitsAwareDataArray.swap_dims(dims_dict: Mapping[Any, Hashable] | None = None, **dims_kwargs) Self

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.

  • **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 – DataArray with swapped dimensions.

Return type:

DataArray

Examples

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

See also

DataArray.rename, Dataset.swap_dims