From b7b8f6b259d118e55cefd4e2413541730706f94c Mon Sep 17 00:00:00 2001 From: Todd Jennings Date: Thu, 26 Mar 2020 21:58:47 -0400 Subject: [PATCH] delegate more to other existing functions --- xarray/core/computation.py | 13 +++++++------ xarray/core/dataarray.py | 8 ++++---- xarray/core/dataset.py | 8 ++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/xarray/core/computation.py b/xarray/core/computation.py index fb3b1ab8ca7..d723c37b616 100644 --- a/xarray/core/computation.py +++ b/xarray/core/computation.py @@ -1346,9 +1346,9 @@ def _calc_idxminmax( array, func: Callable, dim: Optional[Hashable], - skipna: Optional[bool], + skipna: bool = None, fill_value: Any = None, - keep_attrs: Optional[bool], + keep_attrs: bool = None, **kwargs: Any, ): """Apply common operations for idxmin and idxmax.""" @@ -1379,7 +1379,9 @@ def _calc_idxminmax( array = array.where(~allna, 0) # This will run argmin or argmax. - indx = func(array, dim=dim, axis=None, keep_attrs=False, skipna=skipna, **kwargs) + indx = func( + array, dim=dim, axis=None, keep_attrs=keep_attrs, skipna=skipna, **kwargs + ) # Get the coordinate we want. coordarray = array[dim] @@ -1399,8 +1401,7 @@ def _calc_idxminmax( # The dim is gone but we need to remove the corresponding coordinate. del res.coords[dim] - # Put the attrs back in if needed - if keep_attrs: - res.attrs = array.attrs + # Copy attributes from argmin/argmax, if any + res.attrs = indx.attrs return res diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 24c9e5d48fd..36521d726d8 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -3512,8 +3512,8 @@ def idxmin( self, dim: Hashable = None, skipna: bool = None, - fill_value: Any = np.NaN, - keep_attrs: Optional[bool] = False, + fill_value: Any = dtypes.NA, + keep_attrs: bool = None, **kwargs: Any, ) -> "DataArray": """Return the coordinate of the minimum value along a dimension. @@ -3610,8 +3610,8 @@ def idxmax( self, dim: Hashable = None, skipna: bool = None, - fill_value: Any = np.NaN, - keep_attrs: Optional[bool] = False, + fill_value: Any = dtypes.NA, + keep_attrs: bool = None, **kwargs: Any, ) -> "DataArray": """Return the coordinate of the maximum value along a dimension. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4a32860c3b5..11628272767 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -6098,8 +6098,8 @@ def idxmin( self, dim: Hashable = None, skipna: bool = None, - fill_value: Any = np.NaN, - keep_attrs: bool = False, + fill_value: Any = dtypes.NA, + keep_attrs: bool = None, **kwargs: Any, ) -> "Dataset": """Return the coordinate of the minimum value along a dimension. @@ -6197,8 +6197,8 @@ def idxmax( self, dim: Hashable = None, skipna: bool = None, - fill_value: Any = np.NaN, - keep_attrs: Optional[bool] = False, + fill_value: Any = dtypes.NA, + keep_attrs: bool = None, **kwargs: Any, ) -> "Dataset": """Return the coordinate of the maximum value along a dimension.