Skip to content

Commit

Permalink
bug: ser/df.replace only replaces first occurence with NAs
Browse files Browse the repository at this point in the history
  • Loading branch information
asishm committed Mar 16, 2024
1 parent d1c6da1 commit 0f6dd6f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pandas/core/array_algos/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ def _check_comparison_types(
)

# GH#32621 use mask to avoid comparing to NAs
if isinstance(a, np.ndarray):
if isinstance(a, np.ndarray) and mask is not None:
a = a[mask]

result = op(a)

if isinstance(result, np.ndarray) and mask is not None:
# The shape of the mask can differ to that of the result
# since we may compare only a subset of a's or b's elements
tmp = np.zeros(mask.shape, dtype=np.bool_)
np.place(tmp, mask, result)
result = tmp
result = op(a)

if isinstance(result, np.ndarray):
# The shape of the mask can differ to that of the result
# since we may compare only a subset of a's or b's elements
tmp = np.zeros(mask.shape, dtype=np.bool_)
np.place(tmp, mask, result)
result = tmp
else:
result = op(a)

_check_comparison_types(result, a, b)
return result
Expand Down

0 comments on commit 0f6dd6f

Please sign in to comment.