Skip to content

Commit d93b800

Browse files
committed
Move to zip_longest to include unmatched index levels
1 parent c430c61 commit d93b800

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/indexes/multi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Sequence,
1010
)
1111
from functools import wraps
12+
from itertools import zip_longest
1213
from sys import getsizeof
1314
from typing import (
1415
TYPE_CHECKING,
@@ -591,7 +592,7 @@ def from_tuples(
591592
elif isinstance(tuples, list):
592593
arrays = list(lib.to_object_array_tuples(tuples).T)
593594
else:
594-
arrs = zip(*tuples)
595+
arrs = zip_longest(*tuples)
595596
arrays = cast(list[Sequence[Hashable]], arrs)
596597

597598
return cls.from_arrays(arrays, sortorder=sortorder, names=names)

pandas/tests/indexes/multi/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ def test_from_tuples_with_tuple_label():
409409
result = pd.DataFrame([2, 3], columns=["c"], index=idx)
410410
tm.assert_frame_equal(expected, result)
411411

412+
@pytest.mark.parametrize("keys, expected", (
413+
((("l1",), ("l1","l2")), (("l1", np.nan), ("l1","l2"))),
414+
((("l1","l2",), ("l1",)), (("l1","l2"), ("l1", np.nan))),
415+
))
416+
def test_from_tuples_with_various_tuple_lengths(keys, expected):
417+
# Issue 60695
418+
idx = MultiIndex.from_tuples(keys)
419+
assert tuple(idx) == expected
412420

413421
# ----------------------------------------------------------------------------
414422
# from_product

0 commit comments

Comments
 (0)