Skip to content

Commit d98b37d

Browse files
authored
REF: use np.where instead of maybe_upcast_putmask in nanops (#38130)
1 parent f65f0d3 commit d98b37d

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pandas/core/nanops.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pandas._typing import ArrayLike, Dtype, DtypeObj, F, Scalar
1313
from pandas.compat._optional import import_optional_dependency
1414

15-
from pandas.core.dtypes.cast import maybe_upcast_putmask
1615
from pandas.core.dtypes.common import (
1716
get_dtype,
1817
is_any_int_dtype,
@@ -284,18 +283,20 @@ def _get_values(
284283
"""
285284
# In _get_values is only called from within nanops, and in all cases
286285
# with scalar fill_value. This guarantee is important for the
287-
# maybe_upcast_putmask call below
286+
# np.where call below
288287
assert is_scalar(fill_value)
289288
values = extract_array(values, extract_numpy=True)
290289

291290
mask = _maybe_get_mask(values, skipna, mask)
292291

293292
dtype = values.dtype
294293

294+
datetimelike = False
295295
if needs_i8_conversion(values.dtype):
296296
# changing timedelta64/datetime64 to int64 needs to happen after
297297
# finding `mask` above
298298
values = np.asarray(values.view("i8"))
299+
datetimelike = True
299300

300301
dtype_ok = _na_ok_dtype(dtype)
301302

@@ -306,13 +307,13 @@ def _get_values(
306307
)
307308

308309
if skipna and (mask is not None) and (fill_value is not None):
309-
values = values.copy()
310-
if dtype_ok and mask.any():
311-
np.putmask(values, mask, fill_value)
312-
313-
# promote if needed
314-
else:
315-
values, _ = maybe_upcast_putmask(values, mask, fill_value)
310+
if mask.any():
311+
if dtype_ok or datetimelike:
312+
values = values.copy()
313+
np.putmask(values, mask, fill_value)
314+
else:
315+
# np.where will promote if needed
316+
values = np.where(~mask, values, fill_value)
316317

317318
# return a platform independent precision dtype
318319
dtype_max = dtype

0 commit comments

Comments
 (0)