diff --git a/doc/howdoi.rst b/doc/howdoi.rst index a509de10709..b6374cc5100 100644 --- a/doc/howdoi.rst +++ b/doc/howdoi.rst @@ -43,6 +43,10 @@ How do I ... - :py:attr:`DataArray.data` * - convert to and extract the underlying NumPy array - :py:attr:`DataArray.values` + * - convert to a pandas DataFrame + - :py:attr:`Dataset.to_dataframe` + * - sort values + - :py:attr:`Dataset.sortby` * - find out if my xarray object is wrapping a Dask Array - :py:func:`dask.is_dask_collection` * - know how much memory my object requires diff --git a/doc/user-guide/reshaping.rst b/doc/user-guide/reshaping.rst index 81fd4a6d35e..86dc5fbe51a 100644 --- a/doc/user-guide/reshaping.rst +++ b/doc/user-guide/reshaping.rst @@ -275,7 +275,10 @@ One may sort a DataArray/Dataset via :py:meth:`~xarray.DataArray.sortby` and .. ipython:: python ds = xr.Dataset( - {"A": (("x", "y"), [[1, 2], [3, 4]]), "B": (("x", "y"), [[5, 6], [7, 8]])}, + { + "A": (("x", "y"), [[1, 2], [3, 4]]), + "B": (("x", "y"), [[5, 6], [7, 8]]), + }, coords={"x": ["b", "a"], "y": [1, 0]}, ) dax = xr.DataArray([100, 99], [("x", [0, 1])]) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index fd3f2d6529e..30fc478d26e 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -652,7 +652,7 @@ def to_numpy(self) -> np.ndarray: """ Coerces wrapped data to numpy and returns a numpy.ndarray. - See also + See Also -------- DataArray.as_numpy : Same but returns the surrounding DataArray instead. Dataset.as_numpy @@ -665,7 +665,7 @@ def as_numpy(self: T_DataArray) -> T_DataArray: """ Coerces wrapped data and coordinates into numpy arrays, returning a DataArray. - See also + See Also -------- DataArray.to_numpy : Same but returns only the data as a numpy.ndarray object. Dataset.as_numpy : Converts all variables in a Dataset. @@ -3354,6 +3354,13 @@ def sortby( A new dataarray where all the specified dims are sorted by dim labels. + See Also + -------- + Dataset.sortby + numpy.sort + pandas.sort_values + pandas.sort_index + Examples -------- >>> da = xr.DataArray( diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 43f83c77bc8..5a00539346c 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -6036,6 +6036,32 @@ def sortby(self, variables, ascending=True): sorted : Dataset A new dataset where all the specified dims are sorted by dim labels. + + See Also + -------- + DataArray.sortby + numpy.sort + pandas.sort_values + pandas.sort_index + + Examples + -------- + >>> ds = xr.Dataset( + ... { + ... "A": (("x", "y"), [[1, 2], [3, 4]]), + ... "B": (("x", "y"), [[5, 6], [7, 8]]), + ... }, + ... coords={"x": ["b", "a"], "y": [1, 0]}, + ... ) + >>> ds.sortby("x") + + Dimensions: (x: 2, y: 2) + Coordinates: + * x (x)