@@ -5991,26 +5991,30 @@ def idxmin(
5991
5991
5992
5992
def idxmax (
5993
5993
self ,
5994
- dim : Hashable = None ,
5994
+ dim : Dims = None ,
5995
5995
* ,
5996
5996
skipna : bool | None = None ,
5997
5997
fill_value : Any = dtypes .NA ,
5998
5998
keep_attrs : bool | None = None ,
5999
- ) -> Self :
6000
- """Return the coordinate label of the maximum value along a dimension .
5999
+ ) -> Self | dict [ Hashable , Self ] :
6000
+ """Return the coordinate label of the maximum value along one or more dimensions .
6001
6001
6002
6002
Returns a new `DataArray` named after the dimension with the values of
6003
6003
the coordinate labels along that dimension corresponding to maximum
6004
6004
values along that dimension.
6005
6005
6006
+ If a sequence is passed to 'dim', then result is returned as dict of DataArrays.
6007
+ If a single str is passed to 'dim' then returns a DataArray.
6008
+
6006
6009
In comparison to :py:meth:`~DataArray.argmax`, this returns the
6007
6010
coordinate label while :py:meth:`~DataArray.argmax` returns the index.
6011
+ Internally, this method uses argmax to locate the maximum values.
6008
6012
6009
6013
Parameters
6010
6014
----------
6011
- dim : Hashable, optional
6012
- Dimension over which to apply `idxmax`. This is optional for 1D
6013
- arrays, but required for arrays with 2 or more dimensions .
6015
+ dim : "...", str, Iterable of Hashable or None , optional
6016
+ The dimensions over which to find the maximum. By default, finds maximum over
6017
+ all dimensions if array is 1D, otherwise requires explicit specification .
6014
6018
skipna : bool or None, default: None
6015
6019
If True, skip missing values (as marked by NaN). By default, only
6016
6020
skips missing values for ``float``, ``complex``, and ``object``
@@ -6029,9 +6033,9 @@ def idxmax(
6029
6033
6030
6034
Returns
6031
6035
-------
6032
- reduced : DataArray
6033
- New `DataArray` object with `idxmax` applied to its data and the
6034
- indicated dimension removed.
6036
+ reduced : DataArray or dict of DataArray
6037
+ New `DataArray` object(s) with `idxmax` applied to its data and the
6038
+ indicated dimension(s) removed.
6035
6039
6036
6040
See Also
6037
6041
--------
@@ -6076,15 +6080,40 @@ def idxmax(
6076
6080
array([0., 4., 4.])
6077
6081
Coordinates:
6078
6082
* y (y) int64 24B -1 0 1
6079
- """
6080
- return computation ._calc_idxminmax (
6081
- array = self ,
6082
- func = lambda x , * args , ** kwargs : x .argmax (* args , ** kwargs ),
6083
- dim = dim ,
6084
- skipna = skipna ,
6085
- fill_value = fill_value ,
6086
- keep_attrs = keep_attrs ,
6087
- )
6083
+ >>> array.idxmax(dim=["x", "y"])
6084
+ {'x': <xarray.DataArray 'x' ()> Size: 8B
6085
+ array(0.),
6086
+ 'y': <xarray.DataArray 'y' ()> Size: 8B
6087
+ array(-1)}
6088
+ """
6089
+ # Check if dim is multi-dimensional (either ellipsis or a sequence, not a string or tuple)
6090
+ if (dim is ... or (isinstance (dim , Iterable ) and not isinstance (dim , str ))) and not isinstance (
6091
+ dim , tuple
6092
+ ):
6093
+ # For multiple dimensions, process each dimension separately
6094
+ # This matches the behavior pattern of argmax when given multiple dimensions,
6095
+ # but returns coordinate labels instead of indices
6096
+ result = {}
6097
+ for k in dim if dim is not ... else self .dims :
6098
+ result [k ] = self .idxmax (
6099
+ dim = k ,
6100
+ skipna = skipna ,
6101
+ fill_value = fill_value ,
6102
+ keep_attrs = keep_attrs ,
6103
+ )
6104
+ return result
6105
+ else :
6106
+ # Use the existing implementation for single dimension
6107
+ # This wraps argmax through the _calc_idxminmax helper function
6108
+ # which converts indices to coordinate values
6109
+ return computation ._calc_idxminmax (
6110
+ array = self ,
6111
+ func = lambda x , * args , ** kwargs : x .argmax (* args , ** kwargs ),
6112
+ dim = dim ,
6113
+ skipna = skipna ,
6114
+ fill_value = fill_value ,
6115
+ keep_attrs = keep_attrs ,
6116
+ )
6088
6117
6089
6118
def argmin (
6090
6119
self ,
0 commit comments