Skip to content

Commit

Permalink
BUG (CoW): fix reference tracking in replace_list with None (#59807)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored Sep 15, 2024
1 parent 235e1be commit 3e8ac12
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ def _replace_coerce(
nb = nb.copy()
putmask_inplace(nb.values, mask, value)
return [nb]
return [self]
return [self.copy(deep=False)]
return self.replace(
to_replace=to_replace,
value=value,
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/copy_view/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ def test_replace_list_none():

assert not np.shares_memory(get_array(df, "a"), get_array(df2, "a"))

# replace multiple values that don't actually replace anything with None
# https://github.com/pandas-dev/pandas/issues/59770
df3 = df.replace(["d", "e", "f"], value=None)
tm.assert_frame_equal(df3, df_orig)
assert tm.shares_memory(get_array(df, "a"), get_array(df3, "a"))


def test_replace_list_none_inplace_refs():
df = DataFrame({"a": ["a", "b", "c"]})
Expand Down

0 comments on commit 3e8ac12

Please sign in to comment.