Skip to content

Commit

Permalink
thin: add examples (#6663)
Browse files Browse the repository at this point in the history
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: Emma Marshall <emarshall@cisl-racine.scd.ucar.edu>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: keewis <keewis@users.noreply.github.com>
  • Loading branch information
5 people authored Jun 7, 2022
1 parent 1a0879e commit 56f05c3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ Documentation
examples. (:issue:`6333`, :pull:`6334`)
By `Stan West <https://github.com/stanwest>`_.

- Added examples to :py:meth:`Dataset.thin` and :py:meth:`DataArray.thin`
By `Emma Marshall <https://github.com/e-marshall>`_.

Performance
~~~~~~~~~~~

Expand Down
33 changes: 33 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<xarray.DataArray (x: 2, y: 13)>
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)
<xarray.DataArray (x: 1, y: 5)>
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})
<xarray.DataArray (x: 1, y: 3)>
array([[ 0, 5, 10]])
Coordinates:
* x (x) int64 0
* y (y) int64 0 5 10
See Also
--------
Dataset.thin
Expand Down
36 changes: 36 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<xarray.Dataset>
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)
<xarray.Dataset>
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})
<xarray.DataArray (x: 1, y: 3)>
array([[ 0, 5, 10]])
Coordinates:
* x (x) int64 0
* y (y) int64 0 5 10
See Also
--------
Dataset.head
Expand Down

0 comments on commit 56f05c3

Please sign in to comment.