argmin

UnitsAwareDataArray.argmin(dim: Dims = None, *, axis: int | None = None, keep_attrs: bool | None = None, skipna: bool | None = None) Self | dict[Hashable, Self]

Index or indices of the minimum 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 minima, 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 minimum. By default, finds minimum 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 argmin. 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

Return type:

DataArray or dict of DataArray

See also

Variable.argmin, DataArray.idxmin

Examples

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