@@ -2497,6 +2497,63 @@ def isel(
2497
2497
in this dataset, unless vectorized indexing was triggered by using
2498
2498
an array indexer, in which case the data will be a copy.
2499
2499
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
+
2500
2557
See Also
2501
2558
--------
2502
2559
Dataset.sel
@@ -5922,6 +5979,38 @@ def reduce(
5922
5979
reduced : Dataset
5923
5980
Dataset with this object's DataArrays replaced with new DataArrays
5924
5981
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
5925
6014
"""
5926
6015
if kwargs .get ("axis" , None ) is not None :
5927
6016
raise ValueError (
@@ -8435,8 +8524,52 @@ def argmin(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
8435
8524
-------
8436
8525
result : Dataset
8437
8526
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
+
8438
8570
See Also
8439
8571
--------
8572
+ Dataset.idxmin
8440
8573
DataArray.argmin
8441
8574
"""
8442
8575
if dim is None :
@@ -8494,6 +8627,39 @@ def argmax(self: T_Dataset, dim: Hashable | None = None, **kwargs) -> T_Dataset:
8494
8627
-------
8495
8628
result : Dataset
8496
8629
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
+
8497
8663
See Also
8498
8664
--------
8499
8665
DataArray.argmax
0 commit comments