Skip to content

Commit 24510bd

Browse files
Backport PR #59807: BUG (CoW): fix reference tracking in replace_list with None (#59943)
* BUG (CoW): fix reference tracking in replace_list with None (#59807) (cherry picked from commit 3e8ac12) * correct test to work without CoW
1 parent 7e16420 commit 24510bd

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-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

+9
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,15 @@ 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+
if using_copy_on_write:
392+
assert tm.shares_memory(get_array(df, "a"), get_array(df3, "a"))
393+
else:
394+
assert not tm.shares_memory(get_array(df, "a"), get_array(df3, "a"))
395+
387396

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

0 commit comments

Comments
 (0)