Skip to content

Commit 4b71090

Browse files
PERF: avoid copy in concatenate_array_managers if reindex already copies (#44559)
1 parent ca81e6c commit 4b71090

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pandas/core/internals/array_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ def copy_func(ax):
527527
if deep:
528528
new_arrays = [arr.copy() for arr in self.arrays]
529529
else:
530-
new_arrays = self.arrays
531-
return type(self)(new_arrays, new_axes)
530+
new_arrays = list(self.arrays)
531+
return type(self)(new_arrays, new_axes, verify_integrity=False)
532532

533533
def reindex_indexer(
534534
self: T,

pandas/core/internals/concat.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,16 @@ def _concatenate_array_managers(
7777
# reindex all arrays
7878
mgrs = []
7979
for mgr, indexers in mgrs_indexers:
80+
axis1_made_copy = False
8081
for ax, indexer in indexers.items():
8182
mgr = mgr.reindex_indexer(
8283
axes[ax], indexer, axis=ax, allow_dups=True, use_na_proxy=True
8384
)
85+
if ax == 1 and indexer is not None:
86+
axis1_made_copy = True
87+
if copy and concat_axis == 0 and not axis1_made_copy:
88+
# for concat_axis 1 we will always get a copy through concat_arrays
89+
mgr = mgr.copy()
8490
mgrs.append(mgr)
8591

8692
if concat_axis == 1:
@@ -94,8 +100,6 @@ def _concatenate_array_managers(
94100
# concatting along the columns -> combine reindexed arrays in a single manager
95101
assert concat_axis == 0
96102
arrays = list(itertools.chain.from_iterable([mgr.arrays for mgr in mgrs]))
97-
if copy:
98-
arrays = [x.copy() for x in arrays]
99103

100104
new_mgr = ArrayManager(arrays, [axes[1], axes[0]], verify_integrity=False)
101105
return new_mgr

0 commit comments

Comments
 (0)