@@ -3730,16 +3730,35 @@ def _unravel_argminmax(
3730
3730
self ,
3731
3731
argminmax : Hashable ,
3732
3732
dim : Union [Hashable , Sequence [Hashable ], None ],
3733
- keep_attrs : bool ,
3733
+ axis : Union [int , None ],
3734
+ keep_attrs : Optional [bool ],
3734
3735
skipna : Optional [bool ],
3735
3736
) -> Dict [Hashable , "DataArray" ]:
3736
3737
"""Apply argmin or argmax over one or more dimensions, returning the result as a
3737
3738
dict of DataArray that can be passed directly to isel.
3738
3739
"""
3739
- if dim is None :
3740
+ if dim is None and axis is None :
3741
+ warnings .warn (
3742
+ "Behaviour of argmin/argmax with neither dim nor axis argument will "
3743
+ "change to return a dict of indices of each dimension. To get a "
3744
+ "single, flat index, please use np.argmin(da) or np.argmax(da) instead "
3745
+ "of da.argmin() or da.argmax()." ,
3746
+ DeprecationWarning ,
3747
+ )
3748
+ if dim is ...:
3749
+ # In future, should do this also when (dim is None and axis is None)
3740
3750
dim = self .dims
3741
- if not isinstance (dim , Sequence ) or isinstance (dim , str ):
3742
- dim = (dim ,)
3751
+ if (
3752
+ dim is None
3753
+ or axis is not None
3754
+ or not isinstance (dim , Sequence )
3755
+ or isinstance (dim , str )
3756
+ ):
3757
+ # Return int index if single dimension is passed, and is not part of a
3758
+ # sequence
3759
+ return getattr (self , str (argminmax ))(
3760
+ dim = dim , axis = axis , keep_attrs = keep_attrs , skipna = skipna
3761
+ )
3743
3762
3744
3763
# Get a name for the new dimension that does not conflict with any existing
3745
3764
# dimension
@@ -3771,9 +3790,10 @@ def _unravel_argminmax(
3771
3790
3772
3791
return result
3773
3792
3774
- def indices_min (
3793
+ def argmin (
3775
3794
self ,
3776
3795
dim : Union [Hashable , Sequence [Hashable ]] = None ,
3796
+ axis : Union [int , None ] = None ,
3777
3797
keep_attrs : bool = None ,
3778
3798
skipna : bool = None ,
3779
3799
) -> Dict [Hashable , "DataArray" ]:
@@ -3788,6 +3808,9 @@ def indices_min(
3788
3808
dim : hashable or sequence of hashable, optional
3789
3809
The dimensions over which to find the minimum. By default, finds minimum over
3790
3810
all dimensions.
3811
+ axis : int, optional
3812
+ Axis over which to apply `argmin`. Only one of the 'dim' and 'axis' arguments
3813
+ can be supplied.
3791
3814
keep_attrs : bool, optional
3792
3815
If True, the attributes (`attrs`) will be copied from the original
3793
3816
object to the new one. If False (default), the new object will be
@@ -3815,10 +3838,10 @@ def indices_min(
3815
3838
>>> array.argmin()
3816
3839
<xarray.DataArray ()>
3817
3840
array(2)
3818
- >>> array.indices_min( )
3841
+ >>> array.argmin(... )
3819
3842
{'x': <xarray.DataArray ()>
3820
3843
array(2)}
3821
- >>> array.isel(array.indices_min( ))
3844
+ >>> array.isel(array.argmin(... ))
3822
3845
array(-1)
3823
3846
3824
3847
>>> array = xr.DataArray([[[3, 2, 1], [3, 1, 2], [2, 1, 3]],
@@ -3836,7 +3859,7 @@ def indices_min(
3836
3859
[1, 1, 1],
3837
3860
[0, 0, 1]])
3838
3861
Dimensions without coordinates: y, z
3839
- >>> array.indices_min (dim="x")
3862
+ >>> array.argmin (dim=[ "x"] )
3840
3863
{'x': <xarray.DataArray (y: 3, z: 3)>
3841
3864
array([[1, 0, 0],
3842
3865
[1, 1, 1],
@@ -3846,22 +3869,23 @@ def indices_min(
3846
3869
<xarray.DataArray (y: 3)>
3847
3870
array([ 1, -5, 1])
3848
3871
Dimensions without coordinates: y
3849
- >>> array.indices_min (dim=( "x", "z") )
3872
+ >>> array.argmin (dim=[ "x", "z"] )
3850
3873
{'x': <xarray.DataArray (y: 3)>
3851
3874
array([0, 1, 0])
3852
3875
Dimensions without coordinates: y, 'z': <xarray.DataArray (y: 3)>
3853
3876
array([2, 1, 1])
3854
3877
Dimensions without coordinates: y}
3855
- >>> array.isel(array.indices_min (dim=( "x", "z") ))
3878
+ >>> array.isel(array.argmin (dim=[ "x", "z"] ))
3856
3879
<xarray.DataArray (y: 3)>
3857
3880
array([ 1, -5, 1])
3858
3881
Dimensions without coordinates: y
3859
3882
"""
3860
- return self ._unravel_argminmax ("argmin " , dim , keep_attrs , skipna )
3883
+ return self ._unravel_argminmax ("_argmin_base " , dim , axis , keep_attrs , skipna )
3861
3884
3862
- def indices_max (
3885
+ def argmax (
3863
3886
self ,
3864
3887
dim : Union [Hashable , Sequence [Hashable ]] = None ,
3888
+ axis : Union [int , None ] = None ,
3865
3889
keep_attrs : bool = None ,
3866
3890
skipna : bool = None ,
3867
3891
) -> Dict [Hashable , "DataArray" ]:
@@ -3876,6 +3900,9 @@ def indices_max(
3876
3900
dim : hashable or sequence of hashable, optional
3877
3901
The dimensions over which to find the maximum. By default, finds maximum over
3878
3902
all dimensions.
3903
+ axis : int, optional
3904
+ Axis over which to apply `argmin`. Only one of the 'dim' and 'axis' arguments
3905
+ can be supplied.
3879
3906
keep_attrs : bool, optional
3880
3907
If True, the attributes (`attrs`) will be copied from the original
3881
3908
object to the new one. If False (default), the new object will be
@@ -3903,10 +3930,10 @@ def indices_max(
3903
3930
>>> array.argmax()
3904
3931
<xarray.DataArray ()>
3905
3932
array(3)
3906
- >>> array.indices_max( )
3933
+ >>> array.argmax(... )
3907
3934
{'x': <xarray.DataArray ()>
3908
3935
array(3)}
3909
- >>> array.isel(array.indices_max( ))
3936
+ >>> array.isel(array.argmax(... ))
3910
3937
<xarray.DataArray ()>
3911
3938
array(3)
3912
3939
@@ -3925,7 +3952,7 @@ def indices_max(
3925
3952
[0, 1, 0],
3926
3953
[0, 1, 0]])
3927
3954
Dimensions without coordinates: y, z
3928
- >>> array.indices_max (dim="x")
3955
+ >>> array.argmax (dim=[ "x"] )
3929
3956
{'x': <xarray.DataArray (y: 3, z: 3)>
3930
3957
array([[0, 1, 1],
3931
3958
[0, 1, 0],
@@ -3935,18 +3962,18 @@ def indices_max(
3935
3962
<xarray.DataArray (y: 3)>
3936
3963
array([3, 5, 3])
3937
3964
Dimensions without coordinates: y
3938
- >>> array.indices_max (dim=( "x", "z") )
3965
+ >>> array.argmax (dim=[ "x", "z"] )
3939
3966
{'x': <xarray.DataArray (y: 3)>
3940
3967
array([0, 1, 0])
3941
3968
Dimensions without coordinates: y, 'z': <xarray.DataArray (y: 3)>
3942
3969
array([0, 1, 2])
3943
3970
Dimensions without coordinates: y}
3944
- >>> array.isel(array.indices_max (dim=( "x", "z") ))
3971
+ >>> array.isel(array.argmax (dim=[ "x", "z"] ))
3945
3972
<xarray.DataArray (y: 3)>
3946
3973
array([3, 5, 3])
3947
3974
Dimensions without coordinates: y
3948
3975
"""
3949
- return self ._unravel_argminmax ("argmax " , dim , keep_attrs , skipna )
3976
+ return self ._unravel_argminmax ("_argmax_base " , dim , axis , keep_attrs , skipna )
3950
3977
3951
3978
3952
3979
# priority most be higher than Variable to properly work with binary ufuncs
0 commit comments