Skip to content

Commit b6c5297

Browse files
committed
fix for pandas-dev#60695 fix Series constructor dropping key levels when keys have varying entry counts
1 parent 6bcd303 commit b6c5297

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

pandas/core/indexes/multi.py

+3-18
Original file line numberDiff line numberDiff line change
@@ -540,23 +540,6 @@ def from_tuples(
540540
Returns
541541
-------
542542
MultiIndex
543-
544-
See Also
545-
--------
546-
MultiIndex.from_arrays : Convert list of arrays to MultiIndex.
547-
MultiIndex.from_product : Make a MultiIndex from cartesian product
548-
of iterables.
549-
MultiIndex.from_frame : Make a MultiIndex from a DataFrame.
550-
551-
Examples
552-
--------
553-
>>> tuples = [(1, "red"), (1, "blue"), (2, "red"), (2, "blue")]
554-
>>> pd.MultiIndex.from_tuples(tuples, names=("number", "color"))
555-
MultiIndex([(1, 'red'),
556-
(1, 'blue'),
557-
(2, 'red'),
558-
(2, 'blue')],
559-
names=['number', 'color'])
560543
"""
561544
if not is_list_like(tuples):
562545
raise TypeError("Input must be a list / sequence of tuple-likes.")
@@ -591,7 +574,9 @@ def from_tuples(
591574
elif isinstance(tuples, list):
592575
arrays = list(lib.to_object_array_tuples(tuples).T)
593576
else:
594-
arrs = zip(*tuples)
577+
# Use zip_longest instead of zip to handle tuples of different lengths
578+
from itertools import zip_longest
579+
arrs = zip_longest(*tuples, fillvalue=np.nan)
595580
arrays = cast(list[Sequence[Hashable]], arrs)
596581

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

0 commit comments

Comments
 (0)