Skip to content

CLN: remove unnecessary non-scalar code in maybe_upcast_putmask #33641

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

Merged
merged 10 commits into from
Apr 19, 2020
Merged
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
30 changes: 3 additions & 27 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
"""
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.
Expand Down Expand Up @@ -399,24 +400,6 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
other = np.array(other, dtype=result.dtype)

def changeit():

# try to directly set by expanding our array to full
# length of the boolean
try:
om = other[mask]
except (IndexError, TypeError):
# IndexError occurs in test_upcast when we have a boolean
# mask of the wrong shape
# TypeError occurs in test_upcast when `other` is a bool
pass
else:
om_at = om.astype(result.dtype)
if (om == om_at).all():
new_result = result.values.copy()
new_result[mask] = om_at
result[:] = new_result
return result, False

# we are forced to change the dtype of the result as the input
# isn't compatible
r, _ = maybe_upcast(result, fill_value=other, copy=True)
Expand All @@ -434,15 +417,8 @@ def changeit():

# we have a scalar or len 0 ndarray
# and its nan and we are changing some values
if is_scalar(other) or (isinstance(other, np.ndarray) and other.ndim < 1):
if isna(other):
return changeit()

# we have an ndarray and the masking has nans in it
else:

if isna(other).any():
return changeit()
if isna(other):
return changeit()

try:
np.place(result, mask, other)
Expand Down