Skip to content

Commit 44fd458

Browse files
Docstring examples (#7881)
* 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>
1 parent 77e2db5 commit 44fd458

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Documentation
4040

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

4448
Internal Changes
4549
~~~~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,63 @@ def isel(
24972497
in this dataset, unless vectorized indexing was triggered by using
24982498
an array indexer, in which case the data will be a copy.
24992499
2500+
Examples
2501+
--------
2502+
2503+
>>> dataset = xr.Dataset(
2504+
... {
2505+
... "math_scores": (
2506+
... ["student", "test"],
2507+
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
2508+
... ),
2509+
... "english_scores": (
2510+
... ["student", "test"],
2511+
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
2512+
... ),
2513+
... },
2514+
... coords={
2515+
... "student": ["Alice", "Bob", "Charlie"],
2516+
... "test": ["Test 1", "Test 2", "Test 3"],
2517+
... },
2518+
... )
2519+
2520+
# A specific element from the dataset is selected
2521+
2522+
>>> dataset.isel(student=1, test=0)
2523+
<xarray.Dataset>
2524+
Dimensions: ()
2525+
Coordinates:
2526+
student <U7 'Bob'
2527+
test <U6 'Test 1'
2528+
Data variables:
2529+
math_scores int64 78
2530+
english_scores int64 75
2531+
2532+
# Indexing with a slice using isel
2533+
2534+
>>> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2))
2535+
>>> slice_of_data
2536+
<xarray.Dataset>
2537+
Dimensions: (student: 2, test: 2)
2538+
Coordinates:
2539+
* student (student) <U7 'Alice' 'Bob'
2540+
* test (test) <U6 'Test 1' 'Test 2'
2541+
Data variables:
2542+
math_scores (student, test) int64 90 85 78 80
2543+
english_scores (student, test) int64 88 90 75 82
2544+
2545+
>>> index_array = xr.DataArray([0, 2], dims="student")
2546+
>>> indexed_data = dataset.isel(student=index_array)
2547+
>>> indexed_data
2548+
<xarray.Dataset>
2549+
Dimensions: (student: 2, test: 3)
2550+
Coordinates:
2551+
* student (student) <U7 'Alice' 'Charlie'
2552+
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
2553+
Data variables:
2554+
math_scores (student, test) int64 90 85 92 95 92 98
2555+
english_scores (student, test) int64 88 90 92 93 96 91
2556+
25002557
See Also
25012558
--------
25022559
Dataset.sel
@@ -5922,6 +5979,38 @@ def reduce(
59225979
reduced : Dataset
59235980
Dataset with this object's DataArrays replaced with new DataArrays
59245981
of summarized data and the indicated dimension(s) removed.
5982+
5983+
Examples
5984+
--------
5985+
5986+
>>> dataset = xr.Dataset(
5987+
... {
5988+
... "math_scores": (
5989+
... ["student", "test"],
5990+
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
5991+
... ),
5992+
... "english_scores": (
5993+
... ["student", "test"],
5994+
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
5995+
... ),
5996+
... },
5997+
... coords={
5998+
... "student": ["Alice", "Bob", "Charlie"],
5999+
... "test": ["Test 1", "Test 2", "Test 3"],
6000+
... },
6001+
... )
6002+
6003+
# Calculate the 75th percentile of math scores for each student using np.percentile
6004+
6005+
>>> percentile_scores = dataset.reduce(np.percentile, q=75, dim="test")
6006+
>>> percentile_scores
6007+
<xarray.Dataset>
6008+
Dimensions: (student: 3)
6009+
Coordinates:
6010+
* student (student) <U7 'Alice' 'Bob' 'Charlie'
6011+
Data variables:
6012+
math_scores (student) float64 91.0 82.5 96.5
6013+
english_scores (student) float64 91.0 80.5 94.5
59256014
"""
59266015
if kwargs.get("axis", None) is not None:
59276016
raise ValueError(
@@ -8435,8 +8524,52 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
84358524
-------
84368525
result : Dataset
84378526
8527+
Examples
8528+
--------
8529+
>>> dataset = xr.Dataset(
8530+
... {
8531+
... "math_scores": (
8532+
... ["student", "test"],
8533+
... [[90, 85, 79], [78, 80, 85], [95, 92, 98]],
8534+
... ),
8535+
... "english_scores": (
8536+
... ["student", "test"],
8537+
... [[88, 90, 92], [75, 82, 79], [39, 96, 78]],
8538+
... ),
8539+
... },
8540+
... coords={
8541+
... "student": ["Alice", "Bob", "Charlie"],
8542+
... "test": ["Test 1", "Test 2", "Test 3"],
8543+
... },
8544+
... )
8545+
8546+
# Indices of the minimum values along the 'student' dimension are calculated
8547+
8548+
>>> argmin_indices = dataset.argmin(dim="student")
8549+
8550+
>>> min_score_in_math = dataset["student"].isel(
8551+
... student=argmin_indices["math_scores"]
8552+
... )
8553+
>>> min_score_in_math
8554+
<xarray.DataArray 'student' (test: 3)>
8555+
array(['Bob', 'Bob', 'Alice'], dtype='<U7')
8556+
Coordinates:
8557+
student (test) <U7 'Bob' 'Bob' 'Alice'
8558+
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
8559+
8560+
>>> min_score_in_english = dataset["student"].isel(
8561+
... student=argmin_indices["english_scores"]
8562+
... )
8563+
>>> min_score_in_english
8564+
<xarray.DataArray 'student' (test: 3)>
8565+
array(['Charlie', 'Bob', 'Charlie'], dtype='<U7')
8566+
Coordinates:
8567+
student (test) <U7 'Charlie' 'Bob' 'Charlie'
8568+
* test (test) <U6 'Test 1' 'Test 2' 'Test 3'
8569+
84388570
See Also
84398571
--------
8572+
Dataset.idxmin
84408573
DataArray.argmin
84418574
"""
84428575
if dim is None:
@@ -8494,6 +8627,39 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
84948627
-------
84958628
result : Dataset
84968629
8630+
Examples
8631+
--------
8632+
8633+
>>> dataset = xr.Dataset(
8634+
... {
8635+
... "math_scores": (
8636+
... ["student", "test"],
8637+
... [[90, 85, 92], [78, 80, 85], [95, 92, 98]],
8638+
... ),
8639+
... "english_scores": (
8640+
... ["student", "test"],
8641+
... [[88, 90, 92], [75, 82, 79], [93, 96, 91]],
8642+
... ),
8643+
... },
8644+
... coords={
8645+
... "student": ["Alice", "Bob", "Charlie"],
8646+
... "test": ["Test 1", "Test 2", "Test 3"],
8647+
... },
8648+
... )
8649+
8650+
# Indices of the maximum values along the 'student' dimension are calculated
8651+
8652+
>>> argmax_indices = dataset.argmax(dim="test")
8653+
8654+
>>> argmax_indices
8655+
<xarray.Dataset>
8656+
Dimensions: (student: 3)
8657+
Coordinates:
8658+
* student (student) <U7 'Alice' 'Bob' 'Charlie'
8659+
Data variables:
8660+
math_scores (student) int64 2 2 2
8661+
english_scores (student) int64 2 1 1
8662+
84978663
See Also
84988664
--------
84998665
DataArray.argmax

0 commit comments

Comments
 (0)