Skip to content

Commit 7ffcf9d

Browse files
fujiaxiangsimonjayhawkins
authored andcommitted
BUG: concat not copying index and columns when copy=True (#31119)
1 parent 964400d commit 7ffcf9d

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Reshaping
156156
- Bug in :meth:`DataFrame.pivot_table` when ``margin`` is ``True`` and only ``column`` is defined (:issue:`31016`)
157157
- Fix incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`)
158158
- Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`)
159-
159+
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
160160

161161
Sparse
162162
^^^^^^

pandas/core/indexes/api.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464

6565
def get_objs_combined_axis(
66-
objs, intersect: bool = False, axis=0, sort: bool = True
66+
objs, intersect: bool = False, axis=0, sort: bool = True, copy: bool = False
6767
) -> Index:
6868
"""
6969
Extract combined index: return intersection or union (depending on the
@@ -81,13 +81,15 @@ def get_objs_combined_axis(
8181
The axis to extract indexes from.
8282
sort : bool, default True
8383
Whether the result index should come out sorted or not.
84+
copy : bool, default False
85+
If True, return a copy of the combined index.
8486
8587
Returns
8688
-------
8789
Index
8890
"""
8991
obs_idxes = [obj._get_axis(axis) for obj in objs]
90-
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort)
92+
return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)
9193

9294

9395
def _get_distinct_objs(objs: List[Index]) -> List[Index]:
@@ -105,7 +107,10 @@ def _get_distinct_objs(objs: List[Index]) -> List[Index]:
105107

106108

107109
def _get_combined_index(
108-
indexes: List[Index], intersect: bool = False, sort: bool = False
110+
indexes: List[Index],
111+
intersect: bool = False,
112+
sort: bool = False,
113+
copy: bool = False,
109114
) -> Index:
110115
"""
111116
Return the union or intersection of indexes.
@@ -119,6 +124,8 @@ def _get_combined_index(
119124
calculate the union.
120125
sort : bool, default False
121126
Whether the result index should come out sorted or not.
127+
copy : bool, default False
128+
If True, return a copy of the combined index.
122129
123130
Returns
124131
-------
@@ -143,6 +150,11 @@ def _get_combined_index(
143150
index = index.sort_values()
144151
except TypeError:
145152
pass
153+
154+
# GH 29879
155+
if copy:
156+
index = index.copy()
157+
146158
return index
147159

148160

pandas/core/reshape/concat.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ def _get_new_axes(self) -> List[Index]:
517517
def _get_comb_axis(self, i: int) -> Index:
518518
data_axis = self.objs[0]._get_block_manager_axis(i)
519519
return get_objs_combined_axis(
520-
self.objs, axis=data_axis, intersect=self.intersect, sort=self.sort
520+
self.objs,
521+
axis=data_axis,
522+
intersect=self.intersect,
523+
sort=self.sort,
524+
copy=self.copy,
521525
)
522526

523527
def _get_concat_axis(self) -> Index:

pandas/tests/reshape/test_concat.py

+14
Original file line numberDiff line numberDiff line change
@@ -2750,3 +2750,17 @@ def test_concat_sparse():
27502750
)
27512751
result = pd.concat([a, a], axis=1)
27522752
tm.assert_frame_equal(result, expected)
2753+
2754+
2755+
@pytest.mark.parametrize("test_series", [True, False])
2756+
def test_concat_copy_index(test_series, axis):
2757+
# GH 29879
2758+
if test_series:
2759+
ser = Series([1, 2])
2760+
comb = concat([ser, ser], axis=axis, copy=True)
2761+
assert comb.index is not ser.index
2762+
else:
2763+
df = DataFrame([[1, 2], [3, 4]], columns=["a", "b"])
2764+
comb = concat([df, df], axis=axis, copy=True)
2765+
assert comb.index is not df.index
2766+
assert comb.columns is not df.columns

0 commit comments

Comments
 (0)