From 44fd4587932745cb5e3985bcaee9fe5551833010 Mon Sep 17 00:00:00 2001 From: Harshitha <97012127+harshitha1201@users.noreply.github.com> Date: Tue, 4 Jul 2023 21:06:39 +0530 Subject: [PATCH] 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 7575a1245e19c3dbf813d68400da6fe8b07f66bc. * Revert "xarray.dataset.tail" This reverts commit ee8ba416cc0de808e0f75897f25e9739a1d4bb6f. * Revert "xarray.dataset.head" This reverts commit 82e1161578ce44f24cae2aa5fd6b11a8037c32be. * Revert "xarray.dataset.dropna" This reverts commit 760aa4a8c45e8b84798684b9327415bbe53dc627. * Revert "xarray.dataset.ffill" This reverts commit 848a5fd3a598c5c7d8d3ca38f9abebc93b81ce0c. * Revert "xarray.dataset.bfill" This reverts commit eb7514e6f806209f757012ab82f9246b6e4d2cae. * Revert "xarray.dataset.set_Coords" This reverts commit 5ecfd6380f9096fe8d7b2c0ce4d8befcf2ec3f98. * 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 * what's new.rst updated * Move whatsnew entry to unreleased version --------- Co-authored-by: Tom Nicholas --- doc/whats-new.rst | 4 + xarray/core/dataset.py | 166 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ce2c0a698ac..1667fd2fb9c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -40,6 +40,10 @@ Documentation - Expanded the page on wrapping numpy-like "duck" arrays. (:pull:`7911`) By `Tom Nicholas `_. +- 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 `_ . + Internal Changes ~~~~~~~~~~~~~~~~ diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index f4ba9d4f9fe..0cabe803f7d 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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) + + Dimensions: () + Coordinates: + student >> slice_of_data = dataset.isel(student=slice(0, 2), test=slice(0, 2)) + >>> slice_of_data + + Dimensions: (student: 2, test: 2) + Coordinates: + * student (student) >> index_array = xr.DataArray([0, 2], dims="student") + >>> indexed_data = dataset.isel(student=index_array) + >>> indexed_data + + Dimensions: (student: 2, test: 3) + Coordinates: + * student (student) >> 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 + + Dimensions: (student: 3) + Coordinates: + * student (student) 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 + + array(['Bob', 'Bob', 'Alice'], dtype='>> min_score_in_english = dataset["student"].isel( + ... student=argmin_indices["english_scores"] + ... ) + >>> min_score_in_english + + array(['Charlie', 'Bob', 'Charlie'], dtype=' 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 + + Dimensions: (student: 3) + Coordinates: + * student (student)