@@ -3508,6 +3508,191 @@ def pad(
3508
3508
)
3509
3509
return self ._from_temp_dataset (ds )
3510
3510
3511
+ def idxmin (
3512
+ self ,
3513
+ dim : Hashable = None ,
3514
+ skipna : bool = None ,
3515
+ fill_value : Any = dtypes .NA ,
3516
+ keep_attrs : bool = None ,
3517
+ ) -> "DataArray" :
3518
+ """Return the coordinate of the minimum value along a dimension.
3519
+
3520
+ Returns a new DataArray named after the dimension with the values of
3521
+ the coordinate along that dimension corresponding to minimum value
3522
+ along that dimension.
3523
+
3524
+ In comparison to `argmin`, this returns the coordinate label while
3525
+ `argmin` the index.
3526
+
3527
+ Parameters
3528
+ ----------
3529
+ dim : str, optional
3530
+ Dimension over which to apply `idxmin`. This is optional for 1D
3531
+ arrays, but required for arrays with 2 or more dimensions.
3532
+ skipna : bool or None, default None
3533
+ If True, skip missing values (as marked by NaN). By default, only
3534
+ skips missing values for float dtypes; other dtypes either do not
3535
+ have a sentinel missing value (int) or skipna=True has not been
3536
+ implemented (object, datetime64 or timedelta64).
3537
+ fill_value : Any, default NaN
3538
+ Value to be filled in case all of the values along a dimension are
3539
+ null. By default this is NaN. The fill value and result are
3540
+ automatically converted to a compatible dtype if possible.
3541
+ Ignored if `skipna` is False.
3542
+ keep_attrs : bool, default False
3543
+ If True, the attributes (`attrs`) will be copied from the original
3544
+ object to the new one. If False (default), the new object will be
3545
+ returned without attributes.
3546
+
3547
+ Returns
3548
+ -------
3549
+ reduced : DataArray
3550
+ New DataArray object with `idxmin` applied to its data and the
3551
+ indicated dimension removed.
3552
+
3553
+ Examples
3554
+ --------
3555
+
3556
+ >>> array = xr.DataArray([0, 2, 1, 0, -2], dims="x",
3557
+ ... coords={"x": ['a', 'b', 'c', 'd', 'e']})
3558
+ >>> array.min()
3559
+ <xarray.DataArray ()>
3560
+ array(-2)
3561
+ >>> array.argmin()
3562
+ <xarray.DataArray ()>
3563
+ array(4)
3564
+ >>> array.idxmin()
3565
+ <xarray.DataArray 'x' ()>
3566
+ array('e', dtype='<U1')
3567
+
3568
+ >>> array = xr.DataArray([[2.0, 1.0, 2.0, 0.0, -2.0],
3569
+ ... [-4.0, np.NaN, 2.0, np.NaN, -2.0],
3570
+ ... [np.NaN, np.NaN, 1., np.NaN, np.NaN]],
3571
+ ... dims=["y", "x"],
3572
+ ... coords={"y": [-1, 0, 1],
3573
+ ... "x": np.arange(5.)**2}
3574
+ ... )
3575
+ >>> array.min(dim="x")
3576
+ <xarray.DataArray (y: 3)>
3577
+ array([-2., -4., 1.])
3578
+ Coordinates:
3579
+ * y (y) int64 -1 0 1
3580
+ >>> array.argmin(dim="x")
3581
+ <xarray.DataArray (y: 3)>
3582
+ array([4, 0, 2])
3583
+ Coordinates:
3584
+ * y (y) int64 -1 0 1
3585
+ >>> array.idxmin(dim="x")
3586
+ <xarray.DataArray 'x' (y: 3)>
3587
+ array([16., 0., 4.])
3588
+ Coordinates:
3589
+ * y (y) int64 -1 0 1
3590
+
3591
+ See also
3592
+ --------
3593
+ Dataset.idxmin, DataArray.idxmax, DataArray.min, DataArray.argmin
3594
+ """
3595
+ return computation ._calc_idxminmax (
3596
+ array = self ,
3597
+ func = lambda x , * args , ** kwargs : x .argmin (* args , ** kwargs ),
3598
+ dim = dim ,
3599
+ skipna = skipna ,
3600
+ fill_value = fill_value ,
3601
+ keep_attrs = keep_attrs ,
3602
+ )
3603
+
3604
+ def idxmax (
3605
+ self ,
3606
+ dim : Hashable = None ,
3607
+ skipna : bool = None ,
3608
+ fill_value : Any = dtypes .NA ,
3609
+ keep_attrs : bool = None ,
3610
+ ) -> "DataArray" :
3611
+ """Return the coordinate of the maximum value along a dimension.
3612
+
3613
+ Returns a new DataArray named after the dimension with the values of
3614
+ the coordinate along that dimension corresponding to maximum value
3615
+ along that dimension.
3616
+
3617
+ In comparison to `argmax`, this returns the coordinate label while
3618
+ `argmax` returns the index.
3619
+
3620
+ Parameters
3621
+ ----------
3622
+ dim : str, optional
3623
+ Dimension over which to apply `idxmax`. This is optional for 1D
3624
+ arrays, but required for arrays with 2 or more dimensions.
3625
+ skipna : bool or None, default None
3626
+ If True, skip missing values (as marked by NaN). By default, only
3627
+ skips missing values for float dtypes; other dtypes either do not
3628
+ have a sentinel missing value (int) or skipna=True has not been
3629
+ implemented (object, datetime64 or timedelta64).
3630
+ fill_value : Any, default NaN
3631
+ Value to be filled in case all of the values along a dimension are
3632
+ null. By default this is NaN. The fill value and result are
3633
+ automatically converted to a compatible dtype if possible.
3634
+ Ignored if `skipna` is False.
3635
+ keep_attrs : bool, default False
3636
+ If True, the attributes (`attrs`) will be copied from the original
3637
+ object to the new one. If False (default), the new object will be
3638
+ returned without attributes.
3639
+ Returns
3640
+ -------
3641
+ reduced : DataArray
3642
+ New DataArray object with `idxmax` applied to its data and the
3643
+ indicated dimension removed.
3644
+
3645
+ Examples
3646
+ --------
3647
+
3648
+ >>> array = xr.DataArray([0, 2, 1, 0, -2], dims="x",
3649
+ ... coords={"x": ['a', 'b', 'c', 'd', 'e']})
3650
+ >>> array.max()
3651
+ <xarray.DataArray ()>
3652
+ array(2)
3653
+ >>> array.argmax()
3654
+ <xarray.DataArray ()>
3655
+ array(1)
3656
+ >>> array.idxmax()
3657
+ <xarray.DataArray 'x' ()>
3658
+ array('b', dtype='<U1')
3659
+
3660
+ >>> array = xr.DataArray([[2.0, 1.0, 2.0, 0.0, -2.0],
3661
+ ... [-4.0, np.NaN, 2.0, np.NaN, -2.0],
3662
+ ... [np.NaN, np.NaN, 1., np.NaN, np.NaN]],
3663
+ ... dims=["y", "x"],
3664
+ ... coords={"y": [-1, 0, 1],
3665
+ ... "x": np.arange(5.)**2}
3666
+ ... )
3667
+ >>> array.max(dim="x")
3668
+ <xarray.DataArray (y: 3)>
3669
+ array([2., 2., 1.])
3670
+ Coordinates:
3671
+ * y (y) int64 -1 0 1
3672
+ >>> array.argmax(dim="x")
3673
+ <xarray.DataArray (y: 3)>
3674
+ array([0, 2, 2])
3675
+ Coordinates:
3676
+ * y (y) int64 -1 0 1
3677
+ >>> array.idxmax(dim="x")
3678
+ <xarray.DataArray 'x' (y: 3)>
3679
+ array([0., 4., 4.])
3680
+ Coordinates:
3681
+ * y (y) int64 -1 0 1
3682
+
3683
+ See also
3684
+ --------
3685
+ Dataset.idxmax, DataArray.idxmin, DataArray.max, DataArray.argmax
3686
+ """
3687
+ return computation ._calc_idxminmax (
3688
+ array = self ,
3689
+ func = lambda x , * args , ** kwargs : x .argmax (* args , ** kwargs ),
3690
+ dim = dim ,
3691
+ skipna = skipna ,
3692
+ fill_value = fill_value ,
3693
+ keep_attrs = keep_attrs ,
3694
+ )
3695
+
3511
3696
# this needs to be at the end, or mypy will confuse with `str`
3512
3697
# https://mypy.readthedocs.io/en/latest/common_issues.html#dealing-with-conflicting-names
3513
3698
str = property (StringAccessor )
0 commit comments