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 22 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
173 changes: 173 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,67 @@ 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
--------
# Defined the dataset
>>> 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
>>> second_student_first_test = dataset.isel(student=1, test=0)

# Print the selected element
>>> second_student_first_test
<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

# Create a DataArray for indexing
>>> index_array = xr.DataArray([0, 2], dims="student")

# Use isel with the DataArray for indexing
>>> 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 +5983,56 @@ 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

Calculating skewness of math scores for each student using the ``scipy.stats.skew`` function using the above mentioned dataset

# To use the `skew` function, you need to import it from the `scipy.stats` module
>>> from scipy.stats import skew

# Combine the scores of both subjects into a single variable
>>> combined_scores = xr.concat([dataset["math_scores"], dataset["english_scores"]], dim="subject")

# Calculate the skewness of scores for all students
>>> skewness_scores = combined_scores.reduce(skew, dim=("test", "student"))
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved

>>> skewness_scores
<xarray.DataArray 'math_scores' (subject: 2)>
array([-0.19423043, -0.60125 ])
Dimensions without coordinates: subject

Positive skewed value implies that many students scored low with only a few scoring high ( difficult test).
Conversely, a negative skewed value implies that many students scored high with only a few scoring low (an easier test).

"""
if kwargs.get("axis", None) is not None:
raise ValueError(
Expand Down Expand Up @@ -8435,8 +8546,40 @@ 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

# Prints student names with minimum scores for each test
>>> min_score_in_math = dataset["student"].values[argmin_indices["math_scores"].values]
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved
>>> min_score_in_math
array(['Bob', 'Bob', 'Alice'], dtype='<U7')

>>> min_score_in_english = dataset["student"].values[argmin_indices["english_scores"].values]
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved
>>> min_score_in_english
array(['Charlie', 'Bob', 'Charlie'], dtype='<U7')

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 +8637,36 @@ 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")
TomNicholas marked this conversation as resolved.
Show resolved Hide resolved
harshitha1201 marked this conversation as resolved.
Show resolved Hide resolved
>>> 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