diff --git a/doc/whats-new.rst b/doc/whats-new.rst index eeb768224e6..d8d3382675e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -25,6 +25,10 @@ Bug fixes objects remain unaddressable by weakref in order to save memory. (:issue:`3317`) by `Guido Imperiale `_. +Documentation +~~~~~~~~~~~~~ +- Add examples for :py:meth:`Dataset.swap_dims` and :py:meth:`DataArray.swap_dims`. + By `Justus Magin `_. .. _whats-new.0.13.0: diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index e4379bd50fb..add2b1b6c01 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1453,9 +1453,26 @@ def swap_dims(self, dims_dict: Mapping[Hashable, Hashable]) -> "DataArray": Returns ------- - swapped : Dataset + swapped : DataArray DataArray with swapped dimensions. + Examples + -------- + >>> arr = xr.DataArray(data=[0, 1], dims="x", + coords={"x": ["a", "b"], "y": ("x", [0, 1])}) + >>> arr + + array([0, 1]) + Coordinates: + * x (x) >> arr.swap_dims({"x": "y"}) + + array([0, 1]) + Coordinates: + x (y) >> ds = xr.Dataset(data_vars={"a": ("x", [5, 7]), "b": ("x", [0.1, 2.4])}, + coords={"x": ["a", "b"], "y": ("x", [0, 1])}) + >>> ds + + Dimensions: (x: 2) + Coordinates: + * x (x) >> ds.swap_dims({"x": "y"}) + + Dimensions: (y: 2) + Coordinates: + x (y)