Skip to content

Commit 8e3e036

Browse files
authored
CLN: MultiIndex.union align with Index.union() (#38423)
* CLN: MultiIndex.union to look like Index.union() * Fix failing tests * Move test
1 parent e2dec8d commit 8e3e036

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

pandas/core/indexes/multi.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,18 +3566,19 @@ def union(self, other, sort=None):
35663566
"""
35673567
self._validate_sort_keyword(sort)
35683568
self._assert_can_do_setop(other)
3569-
other, result_names = self._convert_can_do_setop(other)
3569+
other, _ = self._convert_can_do_setop(other)
35703570

3571-
if len(other) == 0 or self.equals(other):
3572-
return self.rename(result_names)
3571+
if not len(other) or self.equals(other):
3572+
return self._get_reconciled_name_object(other)
3573+
3574+
if not len(self):
3575+
return other._get_reconciled_name_object(self)
35733576

35743577
return self._union(other, sort=sort)
35753578

35763579
def _union(self, other, sort):
35773580
other, result_names = self._convert_can_do_setop(other)
35783581

3579-
# TODO: Index.union returns other when `len(self)` is 0.
3580-
35813582
if not is_object_dtype(other.dtype):
35823583
raise NotImplementedError(
35833584
"Can only union MultiIndex with MultiIndex or Index of tuples, "
@@ -3608,10 +3609,10 @@ def _maybe_match_names(self, other):
36083609
"""
36093610
Try to find common names to attach to the result of an operation between
36103611
a and b. Return a consensus list of names if they match at least partly
3611-
or None if they have completely different names.
3612+
or list of None if they have completely different names.
36123613
"""
36133614
if len(self.names) != len(other.names):
3614-
return None
3615+
return [None] * len(self.names)
36153616
names = []
36163617
for a_name, b_name in zip(self.names, other.names):
36173618
if a_name == b_name:

pandas/tests/indexes/multi/test_setops.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,15 @@ def test_union_non_object_dtype_raises():
387387
mi.union(idx)
388388

389389

390+
def test_union_empty_self_different_names():
391+
# GH#38423
392+
mi = MultiIndex.from_arrays([[]])
393+
mi2 = MultiIndex.from_arrays([[1, 2], [3, 4]], names=["a", "b"])
394+
result = mi.union(mi2)
395+
expected = MultiIndex.from_arrays([[1, 2], [3, 4]])
396+
tm.assert_index_equal(result, expected)
397+
398+
390399
@pytest.mark.parametrize(
391400
"method", ["union", "intersection", "difference", "symmetric_difference"]
392401
)
@@ -424,12 +433,12 @@ def test_intersect_with_duplicates(tuples, exp_tuples):
424433
@pytest.mark.parametrize(
425434
"data, names, expected",
426435
[
427-
((1,), None, None),
428-
((1,), ["a"], None),
429-
((1,), ["b"], None),
436+
((1,), None, [None, None]),
437+
((1,), ["a"], [None, None]),
438+
((1,), ["b"], [None, None]),
430439
((1, 2), ["c", "d"], [None, None]),
431440
((1, 2), ["b", "a"], [None, None]),
432-
((1, 2, 3), ["a", "b", "c"], None),
441+
((1, 2, 3), ["a", "b", "c"], [None, None]),
433442
((1, 2), ["a", "c"], ["a", None]),
434443
((1, 2), ["c", "b"], [None, "b"]),
435444
((1, 2), ["a", "b"], ["a", "b"]),

0 commit comments

Comments
 (0)