@@ -1726,19 +1726,58 @@ def data_vars(self) -> DataVariables:
17261726 def set_coords (self : T_Dataset , names : Hashable | Iterable [Hashable ]) -> T_Dataset :
17271727 """Given names of one or more variables, set them as coordinates
17281728
1729- Parameters
1730- ----------
1731- names : hashable or iterable of hashable
1732- Name(s) of variables in this dataset to convert into coordinates.
1729+ Parameters
1730+ ----------
1731+ names : hashable or iterable of hashable
1732+ Name(s) of variables in this dataset to convert into coordinates.
17331733
1734- Returns
1735- -------
1736- Dataset
1734+ Example
1735+ -------
17371736
1738- See Also
1739- --------
1740- Dataset.swap_dims
1741- Dataset.assign_coords
1737+ # Sample dataset
1738+ >>> data = xr.DataArray(
1739+ ... [[1, 2], [3, 4]],
1740+ ... dims=("x", "y"),
1741+ ... coords={"x": [0, 1], "y": [0, 1]},
1742+ ... name="data",
1743+ ... )
1744+ >>> dataset = xr.Dataset(data_vars={"data": data})
1745+
1746+ # Print the dataset before setting coordinates
1747+ >>> dataset
1748+ <xarray.Dataset>
1749+ Dimensions: (x: 2, y: 2)
1750+ Coordinates:
1751+ * x (x) int64 0 1
1752+ * y (y) int64 0 1
1753+ Data variables:
1754+ data (x, y) int64 1 2 3 4
1755+
1756+
1757+ # Set "x" and "y" variables as coordinates
1758+ >>> dataset_coords = dataset.set_coords(["x", "y"])
1759+
1760+ # Print the dataset after setting coordinates
1761+ >>> dataset_coords
1762+ <xarray.Dataset>
1763+ Dimensions: (x: 2, y: 2)
1764+ Coordinates:
1765+ x (x) int64 0 1
1766+ y (y) int64 0 1
1767+ Data variables:
1768+ data (x, y) int64 1 2 3 4
1769+
1770+ In the initial dataset, the "x" and "y" variables are present as dimensions. After calling ``set_coords`` (["x", "y"]), these
1771+ variables are converted to coordinates, as shown in the final dataset.
1772+
1773+ Returns
1774+ -------
1775+ Dataset
1776+
1777+ See Also
1778+ --------
1779+ Dataset.swap_dims
1780+ Dataset.assign_coords
17421781 """
17431782 # TODO: allow inserting new coordinates with this method, like
17441783 # DataFrame.set_index?
0 commit comments