Skip to content

Commit

Permalink
make requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
toddrjen committed Mar 21, 2020
1 parent 925b5c8 commit a63c656
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 51 deletions.
132 changes: 104 additions & 28 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3435,7 +3435,7 @@ def pad(
def _calc_idxminmax(
self,
*,
func: str,
func: Callable,
dim: Optional[Hashable],
axis: Optional[int],
skipna: Optional[bool],
Expand Down Expand Up @@ -3492,8 +3492,8 @@ def _calc_idxminmax(
)

# This will run argmin or argmax.
indx = getattr(array, func)(
dim=dim, axis=None, keep_attrs=False, skipna=skipna, **kwargs
indx = func(
array, dim=dim, axis=None, keep_attrs=False, skipna=skipna, **kwargs
)

# Get the coordinate we want.
Expand Down Expand Up @@ -3527,10 +3527,10 @@ def _calc_idxminmax(

def idxmin(
self,
dim: Optional[Hashable] = None,
axis: Optional[int] = None,
skipna: Optional[bool] = None,
promote: Optional[bool] = None,
dim: Hashable = None,
axis: int = None,
skipna: bool = None,
promote: bool = None,
keep_attrs: Optional[bool] = False,
**kwargs: Any,
) -> "DataArray":
Expand All @@ -3545,41 +3545,79 @@ def idxmin(
Parameters
----------
dim : str (optional)
dim : str, optional
Dimension over which to apply `idxmin`.
axis : int (optional)
axis : int, optional
Axis(es) over which to repeatedly apply `idxmin`. Exactly one of
the 'dim' and 'axis' arguments must be supplied.
skipna : bool, optional
skipna : bool or None, default None
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).{min_count_docs}
promote : bool (optional)
implemented (object, datetime64 or timedelta64).
promote : bool or None, default True
If True (default) dtypes that do not support NaN values will be
automatically promoted to those that do. If False a NaN in the
results will raise a TypeError. If None the result will only be
promoted if a NaN is actually present.
keep_attrs : bool, optional
keep_attrs : bool, default False
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
Additional keyword arguments passed on to the appropriate array
function for calculating `{name}` on this object's data.
function for calculating `idxmin` on this object's data.
Returns
-------
reduced : DataArray
New DataArray object with `idxmin` applied to its data and the
indicated dimension removed.
Examples
--------
>>> array = xr.DataArray([0, 2, 1, 0, -2], dims="x",
... coords={"x": ['a', 'b', 'c', 'd', 'e']})
>>> array.min()
<xarray.DataArray ()>
array(-2)
>>> array.argmin()
<xarray.DataArray ()>
array(4)
>>> array.idxmin()
<xarray.DataArray 'x' ()>
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., np.NaN, np.NaN]],
... dims=["y", "x"],
... coords={"y": [-1, 0, 1],
... "x": np.arange(5.)**2}
... )
>>> array.min(dim="x")
<xarray.DataArray (y: 3)>
array([-2., -4., 1.])
Coordinates:
* y (y) int64 -1 0 1
>>> array.argmin(dim="x")
<xarray.DataArray (y: 3)>
array([4, 0, 2])
Coordinates:
* y (y) int64 -1 0 1
>>> array.idxmin(dim="x")
<xarray.DataArray 'x' (y: 3)>
array([16., 0., 4.])
Coordinates:
* y (y) int64 -1 0 1
See also
--------
Dataset.idxmin, DataArray.idxmax, DataArray.min, DataArray.argmin
"""
return self._calc_idxminmax(
func="argmin",
func=lambda x, *args, **kwargs: x.argmin(*args, **kwargs),
dim=dim,
axis=axis,
skipna=skipna,
Expand All @@ -3590,10 +3628,10 @@ def idxmin(

def idxmax(
self,
dim: Optional[Hashable] = None,
axis: Optional[int] = None,
skipna: Optional[bool] = None,
promote: Optional[bool] = None,
dim: Hashable = None,
axis: int = None,
skipna: bool = None,
promote: bool = None,
keep_attrs: Optional[bool] = False,
**kwargs: Any,
) -> "DataArray":
Expand All @@ -3608,41 +3646,79 @@ def idxmax(
Parameters
----------
dim : str (optional)
Dimension over which to apply `idxmax`.
axis : int (optional)
dim : str, optional
Dimension over which to apply `idxmax`..
axis : int, optional
Axis(es) over which to repeatedly apply `idxmax`. Exactly one of
the 'dim' and 'axis' arguments must be supplied.
skipna : bool (optional)
skipna : bool or None, default None
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).{min_count_docs}
promote : bool (optional)
implemented (object, datetime64 or timedelta64).
promote : bool or None, default True
If True (default) dtypes that do not support NaN values will be
automatically promoted to those that do. If False a NaN in the
results will raise a TypeError. If None the result will only be
promoted if a NaN is actually present.
keep_attrs : bool (optional)
keep_attrs : bool, default False
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
Additional keyword arguments passed on to the appropriate array
function for calculating `{name}` on this object's data.
function for calculating `idxmax` on this object's data.
Returns
-------
reduced : DataArray
New DataArray object with `idxmax` applied to its data and the
indicated dimension removed.
Examples
--------
>>> array = xr.DataArray([0, 2, 1, 0, -2], dims="x",
... coords={"x": ['a', 'b', 'c', 'd', 'e']})
>>> array.max()
<xarray.DataArray ()>
array(2)
>>> array.argmax()
<xarray.DataArray ()>
array(1)
>>> array.idxmax()
<xarray.DataArray 'x' ()>
array('b', 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., np.NaN, np.NaN]],
... dims=["y", "x"],
... coords={"y": [-1, 0, 1],
... "x": np.arange(5.)**2}
... )
>>> array.max(dim="x")
<xarray.DataArray (y: 3)>
array([2., 2., 1.])
Coordinates:
* y (y) int64 -1 0 1
>>> array.argmax(dim="x")
<xarray.DataArray (y: 3)>
array([0, 2, 2])
Coordinates:
* y (y) int64 -1 0 1
>>> array.idxmax(dim="x")
<xarray.DataArray 'x' (y: 3)>
array([0., 4., 4.])
Coordinates:
* y (y) int64 -1 0 1
See also
--------
Dataset.idxmax, DataArray.idxmin, DataArray.max, DataArray.argmax
"""
return self._calc_idxminmax(
func="argmax",
func=lambda x, *args, **kwargs: x.argmax(*args, **kwargs),
dim=dim,
axis=axis,
skipna=skipna,
Expand Down
84 changes: 61 additions & 23 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5923,11 +5923,11 @@ def pad(

def idxmin(
self,
dim: Optional[Hashable] = None,
axis: Optional[int] = None,
skipna: Optional[bool] = None,
promote: Optional[bool] = None,
keep_attrs: Optional[bool] = False,
dim: Hashable = None,
axis: int = None,
skipna: bool = None,
promote: bool = None,
keep_attrs: bool = False,
**kwargs: Any,
) -> "Dataset":
"""Return the coordinate of the minimum value along a dimension.
Expand All @@ -5941,35 +5941,73 @@ def idxmin(
Parameters
----------
dim : str (optional)
dim : str, optional
Dimension over which to apply `idxmin`.
axis : int (optional)
axis : int, optional
Axis(es) over which to repeatedly apply `idxmin`. Exactly one of
the 'dim' and 'axis' arguments must be supplied.
skipna : bool, optional
skipna : bool or None, default None
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).{min_count_docs}
promote : bool (optional)
implemented (object, datetime64 or timedelta64).
promote : bool or None, default True
If True (default) dtypes that do not support NaN values will be
automatically promoted to those that do. If False a NaN in the
results will raise a TypeError. If None the result will only be
promoted if a NaN is actually present.
keep_attrs : bool, optional
keep_attrs : bool, default False
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
Additional keyword arguments passed on to the appropriate array
function for calculating `{name}` on this object's data.
function for calculating `idx` on this object's data.
Returns
-------
reduced : Dataset
New Dataset object with `idxmin` applied to its data and the
indicated dimension removed.
Examples
--------
>>> array1 = xr.DataArray([0, 2, 1, 0, -2], dims="x",
... coords={"x": ['a', 'b', 'c', 'd', 'e']})
>>> array2 = 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., np.NaN, np.NaN]],
... dims=["y", "x"],
... coords={"y": [-1, 0, 1],
... "x": ['a', 'b', 'c', 'd', 'e']}
... )
>>> ds = xr.Dataset({'int': array1, 'float': array2})
>>> ds.min(dim='x')
<xarray.Dataset>
Dimensions: (y: 3)
Coordinates:
* y (y) int64 -1 0 1
Data variables:
int int64 -2
float (y) float64 -2.0 -4.0 1.0
>>> ds.argmin(dim='x')
<xarray.Dataset>
Dimensions: (y: 3)
Coordinates:
* y (y) int64 -1 0 1
Data variables:
int int64 4
float (y) int64 4 0 2
>>> ds.idxmin(dim='x')
<xarray.Dataset>
Dimensions: (y: 3)
Coordinates:
* y (y) int64 -1 0 1
Data variables:
int <U1 'e'
float (y) <U1 'e' 'a' 'c'
See also
--------
DataArray.idxmin, Dataset.idxmax, Dataset.min, Dataset.argmin
Expand All @@ -5986,10 +6024,10 @@ def idxmin(

def idxmax(
self,
dim: Optional[Hashable] = None,
axis: Optional[int] = None,
skipna: Optional[bool] = None,
promote: Optional[bool] = None,
dim: Hashable = None,
axis: int = None,
skipna: bool = None,
promote: bool = None,
keep_attrs: Optional[bool] = False,
**kwargs: Any,
) -> "Dataset":
Expand All @@ -6004,28 +6042,28 @@ def idxmax(
Parameters
----------
dim : str (optional)
dim : str, optional
Dimension over which to apply `idxmax`.
axis : int (optional)
axis : int, optional
Axis(es) over which to repeatedly apply `idxmax`. Exactly one of
the 'dim' and 'axis' arguments must be supplied.
skipna : bool (optional)
skipna : bool or None, default None
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).{min_count_docs}
promote : bool (optional)
implemented (object, datetime64 or timedelta64).
promote : bool or None, default True
If True (default) dtypes that do not support NaN values will be
automatically promoted to those that do. If False a NaN in the
results will raise a TypeError. If None the result will only be
promoted if a NaN is actually present.
keep_attrs : bool (optional)
keep_attrs : bool, default False
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
**kwargs : dict
Additional keyword arguments passed on to the appropriate array
function for calculating `{name}` on this object's data.
function for calculating `idxmax` on this object's data.
Returns
-------
Expand Down

0 comments on commit a63c656

Please sign in to comment.