Skip to content

Commit 2e224c7

Browse files
authored
ENH: Use cow for reindex_like (#50426)
1 parent 8d30b6e commit 2e224c7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4220,7 +4220,7 @@ def reindex_like(
42204220
self: NDFrameT,
42214221
other,
42224222
method: Literal["backfill", "bfill", "pad", "ffill", "nearest"] | None = None,
4223-
copy: bool_t = True,
4223+
copy: bool_t | None = None,
42244224
limit=None,
42254225
tolerance=None,
42264226
) -> NDFrameT:

pandas/tests/copy_view/test_methods.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,24 @@ def test_assign_drop_duplicates(using_copy_on_write, method):
387387
tm.assert_frame_equal(df, df_orig)
388388

389389

390+
def test_reindex_like(using_copy_on_write):
391+
df = DataFrame({"a": [1, 2], "b": "a"})
392+
other = DataFrame({"b": "a", "a": [1, 2]})
393+
394+
df_orig = df.copy()
395+
df2 = df.reindex_like(other)
396+
397+
if using_copy_on_write:
398+
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
399+
else:
400+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
401+
402+
df2.iloc[0, 1] = 0
403+
if using_copy_on_write:
404+
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
405+
tm.assert_frame_equal(df, df_orig)
406+
407+
390408
def test_reorder_levels(using_copy_on_write):
391409
index = MultiIndex.from_tuples(
392410
[(1, 1), (1, 2), (2, 1), (2, 2)], names=["one", "two"]

0 commit comments

Comments
 (0)