Skip to content

Commit

Permalink
Docstring examples (#7881)
Browse files Browse the repository at this point in the history
* add examples

* changes done to dataset.isel

* Changes done on isel and reduce

* xarray.dataset.tail

* xarray.dataset.head

* xarray.dataset.dropna

* xarray.dataset.ffill

* xarray.dataset.bfill

* xarray.dataset.set_Coords

* xarray.dataset.reset_coords

* Revert "xarray.dataset.reset_coords"

This reverts commit 7575a12.

* Revert "xarray.dataset.tail"

This reverts commit ee8ba41.

* Revert "xarray.dataset.head"

This reverts commit 82e1161.

* Revert "xarray.dataset.dropna"

This reverts commit 760aa4a.

* Revert "xarray.dataset.ffill"

This reverts commit 848a5fd.

* Revert "xarray.dataset.bfill"

This reverts commit eb7514e.

* Revert "xarray.dataset.set_Coords"

This reverts commit 5ecfd63.

* check pre-commit

* changes

* argmin_indices

* Try adding blank lines

* indentation change

* indentation

* removed dataset.reduce 'skew' example

* doctests

* change

* argmin

* Update xarray/core/dataset.py

Co-authored-by: Tom Nicholas <thomas.nicholas@columbia.edu>

* what's new.rst updated

* Move whatsnew entry to unreleased version

---------

Co-authored-by: Tom Nicholas <thomas.nicholas@columbia.edu>
  • Loading branch information
harshitha1201 and TomNicholas authored Jul 4, 2023
1 parent 77e2db5 commit 44fd458
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Documentation

- Expanded the page on wrapping numpy-like "duck" arrays.
(:pull:`7911`) By `Tom Nicholas <https://github.com/TomNicholas>`_.
- Added examples to docstrings of :py:meth:`Dataset.isel`, :py:meth:`Dataset.reduce`, :py:meth:`Dataset.argmin`,
:py:meth:`Dataset.argmax` (:issue:`6793`, :pull:`7881`)
By `Harshitha <https://github.com/harshitha1201>`_ .


Internal Changes
~~~~~~~~~~~~~~~~
Expand Down
166 changes: 166 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,63 @@ def isel(
in this dataset, unless vectorized indexing was triggered by using
an array indexer, in which case the data will be a copy.
Examples
--------
>>> dataset = xr.Dataset(
... {
... "math_scores": (
... ["student", "test"],
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
... ),
... "english_scores": (
... ["student", "test"],
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
... ),
... },
... coords={
... "student": ["Alice", "Bob", "Charlie"],
... "test": ["Test 1", "Test 2", "Test 3"],
... },
... )
# A specific element from the dataset is selected
>>> dataset.isel(student=1, test=0)
<xarray.Dataset>
Dimensions: ()
Coordinates:
student <U7 'Bob'
test <U6 'Test 1'
Data variables:
math_scores int64 78
english_scores int64 75
# Indexing with a slice using isel
>>> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2))
>>> slice_of_data
<xarray.Dataset>
Dimensions: (student: 2, test: 2)
Coordinates:
* student (student) <U7 'Alice' 'Bob'
* test (test) <U6 'Test 1' 'Test 2'
Data variables:
math_scores (student, test) int64 90 85 78 80
english_scores (student, test) int64 88 90 75 82
>>> index_array = xr.DataArray([0, 2], dims="student")
>>> indexed_data = dataset.isel(student=index_array)
>>> indexed_data
<xarray.Dataset>
Dimensions: (student: 2, test: 3)
Coordinates:
* student (student) <U7 'Alice' 'Charlie'
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
Data variables:
math_scores (student, test) int64 90 85 92 95 92 98
english_scores (student, test) int64 88 90 92 93 96 91
See Also
--------
Dataset.sel
Expand Down Expand Up @@ -5922,6 +5979,38 @@ def reduce(
reduced : Dataset
Dataset with this object's DataArrays replaced with new DataArrays
of summarized data and the indicated dimension(s) removed.
Examples
--------
>>> dataset = xr.Dataset(
... {
... "math_scores": (
... ["student", "test"],
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
... ),
... "english_scores": (
... ["student", "test"],
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
... ),
... },
... coords={
... "student": ["Alice", "Bob", "Charlie"],
... "test": ["Test 1", "Test 2", "Test 3"],
... },
... )
# Calculate the 75th percentile of math scores for each student using np.percentile
>>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test")
>>> percentile_scores
<xarray.Dataset>
Dimensions: (student: 3)
Coordinates:
* student (student) <U7 'Alice' 'Bob' 'Charlie'
Data variables:
math_scores (student) float64 91.0 82.5 96.5
english_scores (student) float64 91.0 80.5 94.5
"""
if kwargs.get("axis", None) is not None:
raise ValueError(
Expand Down Expand Up @@ -8435,8 +8524,52 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
-------
result : Dataset
Examples
--------
>>> dataset = xr.Dataset(
... {
... "math_scores": (
... ["student", "test"],
... [[90, 85, 79], [78, 80, 85], [95, 92, 98]],
... ),
... "english_scores": (
... ["student", "test"],
... [[88, 90, 92], [75, 82, 79], [39, 96, 78]],
... ),
... },
... coords={
... "student": ["Alice", "Bob", "Charlie"],
... "test": ["Test 1", "Test 2", "Test 3"],
... },
... )
# Indices of the minimum values along the 'student' dimension are calculated
>>> argmin_indices = dataset.argmin(dim="student")
>>> min_score_in_math = dataset["student"].isel(
... student=argmin_indices["math_scores"]
... )
>>> min_score_in_math
<xarray.DataArray 'student' (test: 3)>
array(['Bob', 'Bob', 'Alice'], dtype='<U7')
Coordinates:
student (test) <U7 'Bob' 'Bob' 'Alice'
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
>>> min_score_in_english = dataset["student"].isel(
... student=argmin_indices["english_scores"]
... )
>>> min_score_in_english
<xarray.DataArray 'student' (test: 3)>
array(['Charlie', 'Bob', 'Charlie'], dtype='<U7')
Coordinates:
student (test) <U7 'Charlie' 'Bob' 'Charlie'
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
See Also
--------
Dataset.idxmin
DataArray.argmin
"""
if dim is None:
Expand Down Expand Up @@ -8494,6 +8627,39 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
-------
result : Dataset
Examples
--------
>>> dataset = xr.Dataset(
... {
... "math_scores": (
... ["student", "test"],
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
... ),
... "english_scores": (
... ["student", "test"],
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
... ),
... },
... coords={
... "student": ["Alice", "Bob", "Charlie"],
... "test": ["Test 1", "Test 2", "Test 3"],
... },
... )
# Indices of the maximum values along the 'student' dimension are calculated
>>> argmax_indices = dataset.argmax(dim="test")
>>> argmax_indices
<xarray.Dataset>
Dimensions: (student: 3)
Coordinates:
* student (student) <U7 'Alice' 'Bob' 'Charlie'
Data variables:
math_scores (student) int64 2 2 2
english_scores (student) int64 2 1 1
See Also
--------
DataArray.argmax
Expand Down

0 comments on commit 44fd458

Please sign in to comment.