Skip to content

Commit 272a690

Browse files
committed
convert promote argument to fill_value
1 parent cb740cd commit 272a690

File tree

4 files changed

+326
-257
lines changed

4 files changed

+326
-257
lines changed

xarray/core/computation.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import numpy as np
2525

26-
from . import dtypes, duck_array_ops, utils
26+
from . import duck_array_ops, utils
2727
from .alignment import deep_align
2828
from .merge import merge_coordinates_without_align
2929
from .nanops import dask_array
@@ -1347,7 +1347,7 @@ def _calc_idxminmax(
13471347
func: Callable,
13481348
dim: Optional[Hashable],
13491349
skipna: Optional[bool],
1350-
promote: Optional[bool],
1350+
fill_value: Any = None,
13511351
keep_attrs: Optional[bool],
13521352
**kwargs: Any,
13531353
):
@@ -1377,19 +1377,6 @@ def _calc_idxminmax(
13771377
# Need to skip NaN values since argmin and argmax can't handle them
13781378
allna = array.isnull().all(dim)
13791379
array = array.where(~allna, 0)
1380-
hasna = allna.any()
1381-
else:
1382-
allna = None
1383-
hasna = False
1384-
1385-
# If promote is None we only promote if there are NaN values.
1386-
if promote is None:
1387-
promote = hasna
1388-
1389-
if not promote and hasna and array.coords[dim].dtype.kind not in na_dtypes:
1390-
raise TypeError(
1391-
"NaN values present for NaN-incompatible dtype and Promote=False"
1392-
)
13931380

13941381
# This will run argmin or argmax.
13951382
indx = func(array, dim=dim, axis=None, keep_attrs=False, skipna=skipna, **kwargs)
@@ -1405,13 +1392,8 @@ def _calc_idxminmax(
14051392
indx,
14061393
]
14071394

1408-
# Promote to a dtype that can handle NaN values if needed.
1409-
newdtype, fill_value = dtypes.maybe_promote(res.dtype)
1410-
if promote and newdtype != res.dtype:
1411-
res = res.astype(newdtype)
1412-
1413-
# Put the NaN values back in after removing them, if necessary.
1414-
if hasna and allna is not None:
1395+
if skipna or (skipna is None and array.dtype.kind in na_dtypes):
1396+
# Put the NaN values back in after removing them
14151397
res = res.where(~allna, fill_value)
14161398

14171399
# The dim is gone but we need to remove the corresponding coordinate.

xarray/core/dataarray.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,7 +3509,7 @@ def idxmin(
35093509
self,
35103510
dim: Hashable = None,
35113511
skipna: bool = None,
3512-
promote: bool = None,
3512+
fill_value: Any = np.NaN,
35133513
keep_attrs: Optional[bool] = False,
35143514
**kwargs: Any,
35153515
) -> "DataArray":
@@ -3532,11 +3532,11 @@ def idxmin(
35323532
skips missing values for float dtypes; other dtypes either do not
35333533
have a sentinel missing value (int) or skipna=True has not been
35343534
implemented (object, datetime64 or timedelta64).
3535-
promote : bool or None, default True
3536-
If True (default) dtypes that do not support NaN values will be
3537-
automatically promoted to those that do. If False a NaN in the
3538-
results will raise a TypeError. If None the result will only be
3539-
promoted if a NaN is actually present.
3535+
fill_value : Any, default NaN
3536+
Value to be filled in case all of the values along a dimension are
3537+
null. By default this is NaN. The fill value and result are
3538+
automatically converted to a compatible dtype if possible.
3539+
Ignored if `skipna` is False.
35403540
keep_attrs : bool, default False
35413541
If True, the attributes (`attrs`) will be copied from the original
35423542
object to the new one. If False (default), the new object will be
@@ -3598,7 +3598,7 @@ def idxmin(
35983598
func=lambda x, *args, **kwargs: x.argmin(*args, **kwargs),
35993599
dim=dim,
36003600
skipna=skipna,
3601-
promote=promote,
3601+
fill_value=fill_value,
36023602
keep_attrs=keep_attrs,
36033603
**kwargs,
36043604
)
@@ -3607,7 +3607,7 @@ def idxmax(
36073607
self,
36083608
dim: Hashable = None,
36093609
skipna: bool = None,
3610-
promote: bool = None,
3610+
fill_value: Any = np.NaN,
36113611
keep_attrs: Optional[bool] = False,
36123612
**kwargs: Any,
36133613
) -> "DataArray":
@@ -3630,11 +3630,11 @@ def idxmax(
36303630
skips missing values for float dtypes; other dtypes either do not
36313631
have a sentinel missing value (int) or skipna=True has not been
36323632
implemented (object, datetime64 or timedelta64).
3633-
promote : bool or None, default True
3634-
If True (default) dtypes that do not support NaN values will be
3635-
automatically promoted to those that do. If False a NaN in the
3636-
results will raise a TypeError. If None the result will only be
3637-
promoted if a NaN is actually present.
3633+
fill_value : Any, default NaN
3634+
Value to be filled in case all of the values along a dimension are
3635+
null. By default this is NaN. The fill value and result are
3636+
automatically converted to a compatible dtype if possible.
3637+
Ignored if `skipna` is False.
36383638
keep_attrs : bool, default False
36393639
If True, the attributes (`attrs`) will be copied from the original
36403640
object to the new one. If False (default), the new object will be
@@ -3696,7 +3696,7 @@ def idxmax(
36963696
func=lambda x, *args, **kwargs: x.argmax(*args, **kwargs),
36973697
dim=dim,
36983698
skipna=skipna,
3699-
promote=promote,
3699+
fill_value=fill_value,
37003700
keep_attrs=keep_attrs,
37013701
**kwargs,
37023702
)

xarray/core/dataset.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6098,7 +6098,7 @@ def idxmin(
60986098
self,
60996099
dim: Hashable = None,
61006100
skipna: bool = None,
6101-
promote: bool = None,
6101+
fill_value: Any = np.NaN,
61026102
keep_attrs: bool = False,
61036103
**kwargs: Any,
61046104
) -> "Dataset":
@@ -6121,11 +6121,11 @@ def idxmin(
61216121
skips missing values for float dtypes; other dtypes either do not
61226122
have a sentinel missing value (int) or skipna=True has not been
61236123
implemented (object, datetime64 or timedelta64).
6124-
promote : bool or None, default True
6125-
If True (default) dtypes that do not support NaN values will be
6126-
automatically promoted to those that do. If False a NaN in the
6127-
results will raise a TypeError. If None the result will only be
6128-
promoted if a NaN is actually present.
6124+
fill_value : Any, default NaN
6125+
Value to be filled in case all of the values along a dimension are
6126+
null. By default this is NaN. The fill value and result are
6127+
automatically converted to a compatible dtype if possible.
6128+
Ignored if `skipna` is False.
61296129
keep_attrs : bool, default False
61306130
If True, the attributes (`attrs`) will be copied from the original
61316131
object to the new one. If False (default), the new object will be
@@ -6187,7 +6187,7 @@ def idxmin(
61876187
"idxmin",
61886188
dim=dim,
61896189
skipna=skipna,
6190-
promote=promote,
6190+
fill_value=fill_value,
61916191
keep_attrs=keep_attrs,
61926192
**kwargs,
61936193
),
@@ -6197,7 +6197,7 @@ def idxmax(
61976197
self,
61986198
dim: Hashable = None,
61996199
skipna: bool = None,
6200-
promote: bool = None,
6200+
fill_value: Any = np.NaN,
62016201
keep_attrs: Optional[bool] = False,
62026202
**kwargs: Any,
62036203
) -> "Dataset":
@@ -6220,11 +6220,11 @@ def idxmax(
62206220
skips missing values for float dtypes; other dtypes either do not
62216221
have a sentinel missing value (int) or skipna=True has not been
62226222
implemented (object, datetime64 or timedelta64).
6223-
promote : bool or None, default True
6224-
If True (default) dtypes that do not support NaN values will be
6225-
automatically promoted to those that do. If False a NaN in the
6226-
results will raise a TypeError. If None the result will only be
6227-
promoted if a NaN is actually present.
6223+
fill_value : Any, default NaN
6224+
Value to be filled in case all of the values along a dimension are
6225+
null. By default this is NaN. The fill value and result are
6226+
automatically converted to a compatible dtype if possible.
6227+
Ignored if `skipna` is False.
62286228
keep_attrs : bool, default False
62296229
If True, the attributes (`attrs`) will be copied from the original
62306230
object to the new one. If False (default), the new object will be
@@ -6248,7 +6248,7 @@ def idxmax(
62486248
"idxmax",
62496249
dim=dim,
62506250
skipna=skipna,
6251-
promote=promote,
6251+
fill_value=fill_value,
62526252
keep_attrs=keep_attrs,
62536253
**kwargs,
62546254
),

0 commit comments

Comments
 (0)