Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: remove maybe_upcast_putmask #40756

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,55 +498,6 @@ def maybe_cast_to_extension_array(
return result


def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray) -> np.ndarray:
"""
A safe version of putmask that potentially upcasts the result.

The result is replaced with the first N elements of other,
where N is the number of True values in mask.
If the length of other is shorter than N, other will be repeated.

Parameters
----------
result : ndarray
The destination array. This will be mutated in-place if no upcasting is
necessary.
mask : np.ndarray[bool]

Returns
-------
result : ndarray

Examples
--------
>>> arr = np.arange(1, 6)
>>> mask = np.array([False, True, False, True, True])
>>> result = maybe_upcast_putmask(arr, mask)
>>> result
array([ 1., nan, 3., nan, nan])
"""
if not isinstance(result, np.ndarray):
raise ValueError("The result input must be a ndarray.")

# NB: we never get here with result.dtype.kind in ["m", "M"]

if mask.any():

# we want to decide whether place will work
# if we have nans in the False portion of our mask then we need to
# upcast (possibly), otherwise we DON't want to upcast (e.g. if we
# have values, say integers, in the success portion then it's ok to not
# upcast)
new_dtype = ensure_dtype_can_hold_na(result.dtype)

if new_dtype != result.dtype:
result = result.astype(new_dtype, copy=True)

np.place(result, mask, np.nan)

return result


@overload
def ensure_dtype_can_hold_na(dtype: np.dtype) -> np.dtype:
...
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pandas.core.dtypes.cast import (
construct_1d_object_array_from_listlike,
find_common_type,
maybe_upcast_putmask,
)
from pandas.core.dtypes.common import (
ensure_object,
Expand Down Expand Up @@ -129,7 +128,7 @@ def _masked_arith_op(x: np.ndarray, y, op):
if mask.any():
result[mask] = op(xrav[mask], y)

result = maybe_upcast_putmask(result, ~mask)
np.putmask(result, ~mask, np.nan)
result = result.reshape(x.shape) # 2D compat
return result

Expand Down
37 changes: 0 additions & 37 deletions pandas/tests/dtypes/cast/test_upcast.py

This file was deleted.