Skip to content

BUG: concat not copying index and columns when copy=True #31119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ Reshaping
- Bug in :func:`merge_asof` merging on a tz-aware ``left_index`` and ``right_on`` a tz-aware column (:issue:`29864`)
- Improved error message and docstring in :func:`cut` and :func:`qcut` when `labels=True` (:issue:`13318`)
- Bug in missing `fill_na` parameter to :meth:`DataFrame.unstack` with list of levels (:issue:`30740`)
- Bug in :func:`concat` index and columns not copied when ``copy=True`` (:issue:`29879`)

Sparse
^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,12 @@ def _get_new_axes(self) -> List[Index]:

def _get_comb_axis(self, i: int) -> Index:
data_axis = self.objs[0]._get_block_manager_axis(i)
return get_objs_combined_axis(
comb_axis = get_objs_combined_axis(
self.objs, axis=data_axis, intersect=self.intersect, sort=self.sort
)
# GH 29879
# No need for deep copy. Indexes are immutable so they can share underlying data
return comb_axis.copy(deep=False) if self.copy else comb_axis

def _get_concat_axis(self) -> Index:
"""
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2750,3 +2750,16 @@ def test_concat_sparse():
)
result = pd.concat([a, a], axis=1)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("axis", [0, 1])
def test_concat_copy_index(axis):
# GH 29879
df = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "b"])
df_comb = pd.concat([df, df], axis=axis, copy=True)
ser = df["a"]
ser_comb = pd.concat([ser, ser], axis=axis, copy=True)

assert df_comb.index is not df.index
assert df_comb.columns is not df.columns
assert ser_comb.index is not ser.index