Skip to content

FIX BUG: Series constructor from dictionary drops key (index) levels when not all keys have same number of entries #60695 #60814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
3 changes: 2 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Sequence,
)
from functools import wraps
from itertools import zip_longest
from sys import getsizeof
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -591,7 +592,7 @@ def from_tuples(
elif isinstance(tuples, list):
arrays = list(lib.to_object_array_tuples(tuples).T)
else:
arrs = zip(*tuples)
arrs = zip_longest(*tuples)
arrays = cast(list[Sequence[Hashable]], arrs)

return cls.from_arrays(arrays, sortorder=sortorder, names=names)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/multi/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ def test_from_tuples_with_tuple_label():
result = pd.DataFrame([2, 3], columns=["c"], index=idx)
tm.assert_frame_equal(expected, result)

@pytest.mark.parametrize("keys, expected", (
((("l1",), ("l1","l2")), (("l1", np.nan), ("l1","l2"))),
((("l1","l2",), ("l1",)), (("l1","l2"), ("l1", np.nan))),
))
def test_from_tuples_with_various_tuple_lengths(keys, expected):
# Issue 60695
idx = MultiIndex.from_tuples(keys)
assert tuple(idx) == expected

# ----------------------------------------------------------------------------
# from_product
Expand Down
Loading