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