Skip to content

Commit a22f9b9

Browse files
committed
fixed bug 60695
1 parent 6bcd303 commit a22f9b9

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-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, fillvalue=np.nan)
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ def test_from_tuples_with_tuple_label():
410410
tm.assert_frame_equal(expected, result)
411411

412412

413+
@pytest.mark.parametrize(
414+
"keys, expected",
415+
(
416+
((("l1",), ("l1", "l2")), (("l1", np.nan), ("l1", "l2"))),
417+
(
418+
(
419+
(
420+
"l1",
421+
"l2",
422+
),
423+
("l1",),
424+
),
425+
(("l1", "l2"), ("l1", np.nan)),
426+
),
427+
),
428+
)
429+
def test_from_tuples_with_various_tuple_lengths(keys, expected):
430+
# GH 60695
431+
idx = MultiIndex.from_tuples(keys)
432+
assert tuple(idx) == expected
433+
434+
413435
# ----------------------------------------------------------------------------
414436
# from_product
415437
# ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)