Skip to content

Commit cc84a23

Browse files
authored
CLN: remove maybe_upcast_putmask (#40756)
1 parent 6adb2a2 commit cc84a23

File tree

3 files changed

+1
-88
lines changed

3 files changed

+1
-88
lines changed

pandas/core/dtypes/cast.py

-49
Original file line numberDiff line numberDiff line change
@@ -498,55 +498,6 @@ def maybe_cast_to_extension_array(
498498
return result
499499

500500

501-
def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
502-
"""
503-
A safe version of putmask that potentially upcasts the result.
504-
505-
The result is replaced with the first N elements of other,
506-
where N is the number of True values in mask.
507-
If the length of other is shorter than N, other will be repeated.
508-
509-
Parameters
510-
----------
511-
result : ndarray
512-
The destination array. This will be mutated in-place if no upcasting is
513-
necessary.
514-
mask : np.ndarray[bool]
515-
516-
Returns
517-
-------
518-
result : ndarray
519-
520-
Examples
521-
--------
522-
>>> arr = np.arange(1, 6)
523-
>>> mask = np.array([False, True, False, True, True])
524-
>>> result = maybe_upcast_putmask(arr, mask)
525-
>>> result
526-
array([ 1., nan, 3., nan, nan])
527-
"""
528-
if not isinstance(result, np.ndarray):
529-
raise ValueError("The result input must be a ndarray.")
530-
531-
# NB: we never get here with result.dtype.kind in ["m", "M"]
532-
533-
if mask.any():
534-
535-
# we want to decide whether place will work
536-
# if we have nans in the False portion of our mask then we need to
537-
# upcast (possibly), otherwise we DON't want to upcast (e.g. if we
538-
# have values, say integers, in the success portion then it's ok to not
539-
# upcast)
540-
new_dtype = ensure_dtype_can_hold_na(result.dtype)
541-
542-
if new_dtype != result.dtype:
543-
result = result.astype(new_dtype, copy=True)
544-
545-
np.place(result, mask, np.nan)
546-
547-
return result
548-
549-
550501
@overload
551502
def ensure_dtype_can_hold_na(dtype: np.dtype) -> np.dtype:
552503
...

pandas/core/ops/array_ops.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from pandas.core.dtypes.cast import (
2424
construct_1d_object_array_from_listlike,
2525
find_common_type,
26-
maybe_upcast_putmask,
2726
)
2827
from pandas.core.dtypes.common import (
2928
ensure_object,
@@ -129,7 +128,7 @@ def _masked_arith_op(x: np.ndarray, y, op):
129128
if mask.any():
130129
result[mask] = op(xrav[mask], y)
131130

132-
result = maybe_upcast_putmask(result, ~mask)
131+
np.putmask(result, ~mask, np.nan)
133132
result = result.reshape(x.shape) # 2D compat
134133
return result
135134

pandas/tests/dtypes/cast/test_upcast.py

-37
This file was deleted.

0 commit comments

Comments
 (0)