diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 3bd46392f71..6477722fbbb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -145,6 +145,9 @@ Documentation examples. (:issue:`6333`, :pull:`6334`) By `Stan West `_. +- Added examples to :py:meth:`Dataset.thin` and :py:meth:`DataArray.thin` + By `Emma Marshall `_. + Performance ~~~~~~~~~~~ diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 1952925d822..fc4ff2d5783 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1474,6 +1474,39 @@ def thin( """Return a new DataArray whose data is given by each `n` value along the specified dimension(s). + Examples + -------- + >>> x_arr = np.arange(0, 26) + >>> x_arr + array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25]) + >>> x = xr.DataArray( + ... np.reshape(x_arr, (2, 13)), + ... dims=("x", "y"), + ... coords={"x": [0, 1], "y": np.arange(0, 13)}, + ... ) + >>> x + + array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], + [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]]) + Coordinates: + * x (x) int64 0 1 + * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 + + >>> + >>> x.thin(3) + + array([[ 0, 3, 6, 9, 12]]) + Coordinates: + * x (x) int64 0 + * y (y) int64 0 3 6 9 12 + >>> x.thin({"x": 2, "y": 5}) + + array([[ 0, 5, 10]]) + Coordinates: + * x (x) int64 0 + * y (y) int64 0 5 10 + See Also -------- Dataset.thin diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 50531873a24..4e2caf8e1cb 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2633,6 +2633,42 @@ def thin( The keyword arguments form of ``indexers``. One of indexers or indexers_kwargs must be provided. + Examples + -------- + >>> x_arr = np.arange(0, 26) + >>> x_arr + array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25]) + >>> x = xr.DataArray( + ... np.reshape(x_arr, (2, 13)), + ... dims=("x", "y"), + ... coords={"x": [0, 1], "y": np.arange(0, 13)}, + ... ) + >>> x_ds = xr.Dataset({"foo": x}) + >>> x_ds + + Dimensions: (x: 2, y: 13) + Coordinates: + * x (x) int64 0 1 + * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 + Data variables: + foo (x, y) int64 0 1 2 3 4 5 6 7 8 9 ... 16 17 18 19 20 21 22 23 24 25 + + >>> x_ds.thin(3) + + Dimensions: (x: 1, y: 5) + Coordinates: + * x (x) int64 0 + * y (y) int64 0 3 6 9 12 + Data variables: + foo (x, y) int64 0 3 6 9 12 + >>> x.thin({"x": 2, "y": 5}) + + array([[ 0, 5, 10]]) + Coordinates: + * x (x) int64 0 + * y (y) int64 0 5 10 + See Also -------- Dataset.head