Skip to content

Commit 0958f5c

Browse files
jbrockmendelfeefladder
authored andcommitted
BUG: ArrayManager reindex with copy=True not copying (pandas-dev#42647)
1 parent 0bece99 commit 0958f5c

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

pandas/core/generic.py

+2
Original file line numberDiff line numberDiff line change
@@ -4840,6 +4840,8 @@ def _reindex_axes(
48404840
copy=copy,
48414841
allow_dups=False,
48424842
)
4843+
# If we've made a copy once, no need to make another one
4844+
copy = False
48434845

48444846
return obj
48454847

pandas/core/internals/array_manager.py

+2
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ def _reindex_indexer(
601601
)
602602
else:
603603
arr = self.arrays[i]
604+
if copy:
605+
arr = arr.copy()
604606
new_arrays.append(arr)
605607

606608
else:

pandas/tests/frame/methods/test_reindex.py

+14
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ class TestDataFrameSelectReindex:
8383
# These are specific reindex-based tests; other indexing tests should go in
8484
# test_indexing
8585

86+
def test_reindex_copies(self):
87+
# based on asv time_reindex_axis1
88+
N = 10
89+
df = DataFrame(np.random.randn(N * 10, N))
90+
cols = np.arange(N)
91+
np.random.shuffle(cols)
92+
93+
result = df.reindex(columns=cols, copy=True)
94+
assert not np.shares_memory(result[0]._values, df[0]._values)
95+
96+
# pass both columns and index
97+
result2 = df.reindex(columns=cols, index=df.index, copy=True)
98+
assert not np.shares_memory(result2[0]._values, df[0]._values)
99+
86100
def test_reindex_date_fill_value(self):
87101
# passing date to dt64 is deprecated
88102
arr = date_range("2016-01-01", periods=6).values.reshape(3, 2)

0 commit comments

Comments
 (0)