Skip to content

#Duplicated names in concatenation #60521

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

Closed
Closed
Changes from all commits
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
36 changes: 35 additions & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,15 @@ def _get_concat_axis_dataframe(
if keys is None:
if levels is not None:
raise ValueError("levels supported only when keys is not None")
concat_axis = _concat_indexes(indexes)
if axis == 0:
concat_axis = _concat_indexes(indexes)
elif axis == 1:
interesected_indexes = indexes[0].intersection(indexes[1])
if interesected_indexes is None:
concat_axis = _concat_indexes(indexes)
else:
indexes = _rename_duplicated_axis_names(indexes, interesected_indexes)
concat_axis = _concat_indexes(indexes)
else:
concat_axis = _make_concat_multiindex(indexes, keys, levels, names)

Expand All @@ -721,6 +729,32 @@ def _get_concat_axis_dataframe(
return concat_axis


def _rename_duplicated_axis_names(
indexes: list[Index], interesected_indexes: Index
) -> list[Index]:
"""
Rename duplicated axis names if there are duplicated values in the indexes.

Args:
indexes (Index): Indexes that should be concatenated and have duplicated values.
interesected_indexes (Index): The set of duplicated values in the indexes.

Returns:
list[Index]: New names for axis without duplicated values.
"""
new_indexes = []
for number, index in enumerate(indexes):
for i in range(len(index)):
if index[i] in interesected_indexes:
new_index = index.drop(index[i])
index = new_index.insert(
i, f"{index[i]}_{number}"
) # New values inserted in the new index
new_indexes.append(index)

return new_indexes


def _clean_keys_and_objs(
objs: Iterable[Series | DataFrame] | Mapping[HashableT, Series | DataFrame],
keys,
Expand Down
Loading