xarray.DataArray.idxmin#
- DataArray.idxmin(dim=None, *, skipna=None, fill_value=<NA>, keep_attrs=None)[source]#
Return the coordinate label of the minimum 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 minimum values along that dimension.
If a sequence of dimensions or
...is passed todim, the minimum is taken over all of them and a dict of DataArray objects, one for each dimension, is returned instead, likeargmin(). The result can be passed tosel().In comparison to
argmin(), this returns the coordinate label whileargmin()returns the index.- Parameters:
dim (
Hashable, sequence ofHashableor..., optional) – Dimension(s) over which to apply idxmin....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 (
boolorNone, default:None) – If True, skip missing values (as marked by NaN). By default, only skips missing values forfloat,complex, andobjectdtypes; other dtypes either do not have a sentinel missing value (int) orskipna=Truehas not been implemented (datetime64ortimedelta64). If False, NaN is treated as the minimum like innumpy.argmin(), 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 ifskipnais False.keep_attrs (
boolorNone, optional) – If True, the attributes (attrs) of the coordinate are copied to the result. If False, the result is returned without attributes.
- Returns:
reduced (
DataArrayordictofDataArray) – New DataArray object with idxmin applied to its data and the indicated dimension removed, or a dict of them mapping each dimension to its coordinate labels ifdimis a sequence or....
See also
Dataset.idxmin,DataArray.idxmax,DataArray.min,DataArray.argminExamples
>>> array = xr.DataArray( ... [0, 2, 1, 0, -2], dims="x", coords={"x": ["a", "b", "c", "d", "e"]} ... ) >>> array.min() <xarray.DataArray ()> Size: 8B array(-2) >>> array.argmin(...) {'x': <xarray.DataArray ()> Size: 8B array(4)} >>> array.idxmin() <xarray.DataArray 'x' ()> Size: 4B array('e', dtype='<U1')
>>> 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.min(dim="x") <xarray.DataArray (y: 3)> Size: 24B array([-2., -4., 1.]) Coordinates: * y (y) int64 24B -1 0 1 >>> array.argmin(dim="x") <xarray.DataArray (y: 3)> Size: 24B array([4, 0, 2]) Coordinates: * y (y) int64 24B -1 0 1 >>> array.idxmin(dim="x") <xarray.DataArray 'x' (y: 3)> Size: 24B array([16., 0., 4.]) Coordinates: * y (y) int64 24B -1 0 1 >>> array.idxmin(dim=["y", "x"]) {'y': <xarray.DataArray 'y' ()> Size: 8B array(0), 'x': <xarray.DataArray 'x' ()> Size: 8B array(0.)}