Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thin: add examples #6663

Merged
merged 20 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
dcherian marked this conversation as resolved.
Show resolved Hide resolved

Examples
dcherian marked this conversation as resolved.
Show resolved Hide resolved
--------
>>> 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