xarray.DataArray.argmax

xarray.DataArray.argmax#

DataArray.argmax(dim=None, *, axis=None, keep_attrs=None, skipna=None)[source]#

Index or indices of the maximum of the DataArray over one or more dimensions.

If a sequence is passed to ‘dim’, then result returned as dict of DataArrays, which can be passed directly to isel(). If a single str is passed to ‘dim’ then returns a DataArray with dtype int.

If there are multiple maxima, the indices of the first one found will be returned.

Parameters:
  • dim ("...", str, Iterable of Hashable or None, optional) – The dimensions over which to find the maximum. By default, finds maximum over all dimensions - for now returning an int for backward compatibility, but this is deprecated, in future will return a dict with indices for all dimensions; to return a dict with all dimensions now, pass ‘…’.

  • axis (int or None, optional) – Axis over which to apply argmax. Only one of the ‘dim’ and ‘axis’ arguments can be supplied.

  • keep_attrs (bool or None, optional) – If True, the attributes (attrs) will be copied from the original object to the new one. If False, the new object will be returned without attributes.

  • skipna (bool or None, optional) – If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64).

Returns:

result (DataArray or dict of DataArray)

Examples

>>> array = xr.DataArray([0, 2, -1, 3], dims="x")
>>> array.max()
<xarray.DataArray ()> Size: 8B
array(3)
>>> array.argmax(...)
{'x': <xarray.DataArray ()> Size: 8B
array(3)}
>>> array.isel(array.argmax(...))
<xarray.DataArray ()> Size: 8B
array(3)
>>> array = xr.DataArray(
...     [[[3, 2, 1], [3, 1, 2], [2, 1, 3]], [[1, 3, 2], [2, 5, 1], [2, 3, 1]]],
...     dims=("x", "y", "z"),
... )
>>> array.max(dim="x")
<xarray.DataArray (y: 3, z: 3)> Size: 72B
array([[3, 3, 2],
       [3, 5, 2],
       [2, 3, 3]])
Dimensions without coordinates: y, z
>>> array.argmax(dim="x")
<xarray.DataArray (y: 3, z: 3)> Size: 72B
array([[0, 1, 1],
       [0, 1, 0],
       [0, 1, 0]])
Dimensions without coordinates: y, z
>>> array.argmax(dim=["x"])
{'x': <xarray.DataArray (y: 3, z: 3)> Size: 72B
array([[0, 1, 1],
       [0, 1, 0],
       [0, 1, 0]])
Dimensions without coordinates: y, z}
>>> array.max(dim=("x", "z"))
<xarray.DataArray (y: 3)> Size: 24B
array([3, 5, 3])
Dimensions without coordinates: y
>>> array.argmax(dim=["x", "z"])
{'x': <xarray.DataArray (y: 3)> Size: 24B
array([0, 1, 0])
Dimensions without coordinates: y, 'z': <xarray.DataArray (y: 3)> Size: 24B
array([0, 1, 2])
Dimensions without coordinates: y}
>>> array.isel(array.argmax(dim=["x", "z"]))
<xarray.DataArray (y: 3)> Size: 24B
array([3, 5, 3])
Dimensions without coordinates: y