Skip to content

Commit

Permalink
#Duplicated names change for columns and 2 dataframes
Browse files Browse the repository at this point in the history
Signed-off-by: kevkle <[email protected]>
  • Loading branch information
kevkle committed Dec 7, 2024
1 parent 8a286fa commit e1ac72a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,13 @@ 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)
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 @@ -720,6 +726,27 @@ 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],
Expand Down

0 comments on commit e1ac72a

Please sign in to comment.