# xarray.DataArray.idxmax

#### DataArray.idxmax(dim=None, \*, skipna=None, fill_value=<NA>, keep_attrs=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L6305-L6414)

Return the coordinate label of the maximum value along one or more dimensions.

Returns a new DataArray named after the dimension with the values of
the coordinate labels along that dimension corresponding to maximum
values along that dimension.

If a sequence of dimensions or `...` is passed to `dim`, the
maximum is taken over all of them and a dict of DataArray objects,
one for each dimension, is returned instead, like
[`argmax()`](xarray.DataArray.argmax.html.md#xarray.DataArray.argmax). The result can be passed to
[`sel()`](xarray.DataArray.sel.html.md#xarray.DataArray.sel).

In comparison to [`argmax()`](xarray.DataArray.argmax.html.md#xarray.DataArray.argmax), this returns the
coordinate label while [`argmax()`](xarray.DataArray.argmax.html.md#xarray.DataArray.argmax) returns the index.

* **Parameters:**
  * **dim** (`Hashable`, [sequence](https://docs.python.org/3/glossary.html#term-sequence) of `Hashable` or [`...`](https://docs.python.org/3/builtins/constants.html#Ellipsis), *optional*) – Dimension(s) over which to apply idxmax. `...` means all
    dimensions. If not given, the only dimension of a 1D array is used,
    and all dimensions of an array with 2 or more dimensions.
  * **skipna** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – If True, skip missing values (as marked by NaN). By default, only
    skips missing values for `float`, `complex`, and `object`
    dtypes; other dtypes either do not have a sentinel missing value
    (`int`) or `skipna=True` has not been implemented
    (`datetime64` or `timedelta64`).
    If False, NaN is treated as the maximum like in
    [`numpy.argmax()`](https://numpy.org/doc/stable/reference/generated/numpy.argmax.html#numpy.argmax), so the label of the first NaN is returned.
  * **fill_value** (`Any`, *default*: `NaN`) – Value to be filled in case all of the values along a dimension are
    null.  By default this is NaN.  The fill value and result are
    automatically converted to a compatible dtype if possible.
    Ignored if `skipna` is False.
  * **keep_attrs** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – If True, the attributes (`attrs`) of the coordinate are copied to
    the result. If False, the result is returned without attributes.
* **Returns:**
  **reduced** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray) or [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) of [`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – New DataArray object with idxmax applied to its data and the
  indicated dimension removed, or a dict of them mapping each
  dimension to its coordinate labels if `dim` is a sequence or
  `...`.

#### SEE ALSO
[`Dataset.idxmax`](xarray.Dataset.idxmax.html.md#xarray.Dataset.idxmax), [`DataArray.idxmin`](xarray.DataArray.idxmin.html.md#xarray.DataArray.idxmin), [`DataArray.max`](xarray.DataArray.max.html.md#xarray.DataArray.max), [`DataArray.argmax`](xarray.DataArray.argmax.html.md#xarray.DataArray.argmax)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('4b453842-8004-472b-b295-dc2ae38f9f91','9c0192e3-1d8a-4dd1-8e3b-defeeb7a2637','fe43bb18-4a40-4848-92be-7599863756f5','../lite/tree/../notebooks/index.html?path=9d24b868_7cb0_4973_9032_ab34253b3466.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> array = xr.DataArray(
...     [0, 2, 1, 0, -2], dims="x", coords={"x": ["a", "b", "c", "d", "e"]}
... )
>>> array.max()
<xarray.DataArray ()> Size: 8B
array(2)
>>> array.argmax(...)
{'x': <xarray.DataArray ()> Size: 8B
array(1)}
>>> array.idxmax()
<xarray.DataArray 'x' ()> Size: 4B
array('b', dtype='<U1')
```

```pycon
>>> array = xr.DataArray(
...     [
...         [2.0, 1.0, 2.0, 0.0, -2.0],
...         [-4.0, np.nan, 2.0, np.nan, -2.0],
...         [np.nan, np.nan, 1.0, np.nan, np.nan],
...     ],
...     dims=["y", "x"],
...     coords={"y": [-1, 0, 1], "x": np.arange(5.0) ** 2},
... )
>>> array.max(dim="x")
<xarray.DataArray (y: 3)> Size: 24B
array([2., 2., 1.])
Coordinates:
  * y        (y) int64 24B -1 0 1
>>> array.argmax(dim="x")
<xarray.DataArray (y: 3)> Size: 24B
array([0, 2, 2])
Coordinates:
  * y        (y) int64 24B -1 0 1
>>> array.idxmax(dim="x")
<xarray.DataArray 'x' (y: 3)> Size: 24B
array([0., 4., 4.])
Coordinates:
  * y        (y) int64 24B -1 0 1
>>> array.idxmax(dim=["y", "x"])
{'y': <xarray.DataArray 'y' ()> Size: 8B
array(-1), 'x': <xarray.DataArray 'x' ()> Size: 8B
array(0.)}
```

<div id="fe43bb18-4a40-4848-92be-7599863756f5" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('4b453842-8004-472b-b295-dc2ae38f9f91','fe43bb18-4a40-4848-92be-7599863756f5')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('4b453842-8004-472b-b295-dc2ae38f9f91','fe43bb18-4a40-4848-92be-7599863756f5')">Open In Tab</button></div><div id="9c0192e3-1d8a-4dd1-8e3b-defeeb7a2637" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
