Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ MultiIndex
- :func:`MultiIndex.get_level_values` accessing a :class:`DatetimeIndex` does not carry the frequency attribute along (:issue:`58327`, :issue:`57949`)
- Bug in :class:`DataFrame` arithmetic operations in case of unaligned MultiIndex columns (:issue:`60498`)
- Bug in :class:`DataFrame` arithmetic operations with :class:`Series` in case of unaligned MultiIndex (:issue:`61009`)
- Bug in :meth:`MultiIndex.union` raising when indexes have duplicates with differing names (:issue:`62059`)
- Bug in :meth:`MultiIndex.from_tuples` causing wrong output with input of type tuples having NaN values (:issue:`60695`, :issue:`60988`)
- Bug in :meth:`DataFrame.__setitem__` where column alignment logic would reindex the assigned value with an empty index, incorrectly setting all values to ``NaN``.(:issue:`61841`)
- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` where reindexing :class:`Index` to a :class:`MultiIndex` would incorrectly set all values to ``NaN``.(:issue:`60923`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3946,7 +3946,9 @@ def _union(self, other, sort) -> MultiIndex:
if other.has_duplicates:
# This is only necessary if other has dupes,
# otherwise difference is faster
result = super()._union(other, sort)
result = super(MultiIndex, self.rename(result_names))._union(
other.rename(result_names), sort
)

if isinstance(result, MultiIndex):
return result
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ def test_union_keep_ea_dtype_with_na(any_numeric_ea_dtype):
tm.assert_index_equal(result, expected)


def test_union_duplicates_different_names():
# GH#62059
mi1 = MultiIndex.from_tuples([(1, "a"), (2, "b")], names=["x", "y"])
mi2 = MultiIndex.from_tuples([(2, "b"), (3, "c"), (2, "b")])

result = mi1.union(mi2)
expected = MultiIndex.from_tuples(
[(1, "a"), (2, "b"), (2, "b"), (3, "c")], names=[None, None]
)
tm.assert_index_equal(result, expected)


@pytest.mark.parametrize(
"levels1, levels2, codes1, codes2, names",
[
Expand Down
Loading