Skip to content

Commit a2a1a91

Browse files
BUG (CoW): fix reference tracking in replace_list with None (pandas-dev#59807)
(cherry picked from commit 3e8ac12)
1 parent 7e16420 commit a2a1a91

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ def _replace_coerce(
12181218
putmask_inplace(nb.values, mask, value)
12191219
return [nb]
12201220
if using_cow:
1221-
return [self]
1221+
return [self.copy(deep=False)]
12221222
return [self] if inplace else [self.copy()]
12231223
return self.replace(
12241224
to_replace=to_replace,

pandas/tests/copy_view/test_replace.py

+6
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ def test_replace_list_none(using_copy_on_write):
384384

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

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

388394
def test_replace_list_none_inplace_refs(using_copy_on_write, warn_copy_on_write):
389395
df = DataFrame({"a": ["a", "b", "c"]})

0 commit comments

Comments
 (0)