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

Docstring examples #7881

Merged
merged 35 commits into from
Jul 4, 2023
Merged
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
70c52b8
add examples
harshitha1201 May 29, 2023
1e403a8
changes done to dataset.isel
harshitha1201 Jun 5, 2023
431f8b7
Changes done on isel and reduce
harshitha1201 Jun 12, 2023
ee8ba41
xarray.dataset.tail
harshitha1201 Jun 20, 2023
82e1161
xarray.dataset.head
harshitha1201 Jun 20, 2023
760aa4a
xarray.dataset.dropna
harshitha1201 Jun 20, 2023
848a5fd
xarray.dataset.ffill
harshitha1201 Jun 20, 2023
eb7514e
xarray.dataset.bfill
harshitha1201 Jun 20, 2023
5ecfd63
xarray.dataset.set_Coords
harshitha1201 Jun 20, 2023
7575a12
xarray.dataset.reset_coords
harshitha1201 Jun 20, 2023
14952fa
Revert "xarray.dataset.reset_coords"
harshitha1201 Jun 22, 2023
3869b78
Revert "xarray.dataset.tail"
harshitha1201 Jun 22, 2023
212c874
Revert "xarray.dataset.head"
harshitha1201 Jun 22, 2023
05c9e66
Revert "xarray.dataset.dropna"
harshitha1201 Jun 22, 2023
4ee8603
Revert "xarray.dataset.ffill"
harshitha1201 Jun 22, 2023
95e27d2
Revert "xarray.dataset.bfill"
harshitha1201 Jun 22, 2023
4811641
Revert "xarray.dataset.set_Coords"
harshitha1201 Jun 22, 2023
20ac818
check pre-commit
harshitha1201 Jun 27, 2023
eae0ae9
changes
harshitha1201 Jun 30, 2023
39880aa
Merge branch 'main' into add-examples
harshitha1201 Jun 30, 2023
0a1a558
argmin_indices
harshitha1201 Jun 30, 2023
c4988b6
Merge branch 'add-examples' of https://github.com/harshitha1201/xarra…
harshitha1201 Jun 30, 2023
6a5bc79
Try adding blank lines
TomNicholas Jun 30, 2023
1ee2c93
indentation change
harshitha1201 Jun 30, 2023
7872104
Merge branch 'add-examples' of https://github.com/harshitha1201/xarra…
harshitha1201 Jun 30, 2023
a5ea816
indentation
harshitha1201 Jun 30, 2023
1be4f82
removed dataset.reduce 'skew' example
harshitha1201 Jul 3, 2023
067a310
doctests
harshitha1201 Jul 3, 2023
460021b
change
harshitha1201 Jul 3, 2023
d60d601
argmin
harshitha1201 Jul 3, 2023
85d3817
Update xarray/core/dataset.py
harshitha1201 Jul 3, 2023
b781f6a
Merge branch 'main' into add-examples
harshitha1201 Jul 3, 2023
4ed9393
what's new.rst updated
harshitha1201 Jul 3, 2023
ade5bb8
Merge branch 'add-examples' of https://github.com/harshitha1201/xarra…
harshitha1201 Jul 3, 2023
42a7cee
Move whatsnew entry to unreleased version
TomNicholas Jul 3, 2023
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
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")
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved

>>> 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
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved
"""
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
--------
TomNicholas marked this conversation as resolved.
Show resolved Hide resolved

>>> 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