Skip to content

BUG: Index allows one item to be list among others that are not #61672

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ Indexing
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
- Bug in creating :class:`Index` with one item being a ``list`` among others thar aren't (should raise ValueError) (:issue:`60925`)

Missing
^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ def __new__(
# Ensure we get 1-D array of tuples instead of 2D array.
data = com.asarray_tuplesafe(data, dtype=_dtype_obj)

#60925 should raise when one of index's items is a list and others are not
if (any(isinstance(el, list) for el in data) and
not all(isinstance(el, list) for el in data)):
raise ValueError(
"Index names must all be hashable, or all lists to make MultiIndex"
)

try:
arr = sanitize_array(data, None, dtype=dtype, copy=copy)
except ValueError as err:
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/frame/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ def test_assign_index_sequences(self):
repr(df)

# this travels an improper code path
# #60925 should raise when one of index's items is a list and others are not
index[0] = ["faz", "boo"]
df.index = index
repr(df)
msg = "Index names must all be hashable, or all lists to make MultiIndex"
with pytest.raises(ValueError, match=msg):
df.index = index
repr(df)

def test_repr_with_mi_nat(self):
df = DataFrame({"X": [1, 2]}, index=[[NaT, Timestamp("20130101")], ["a", "b"]])
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def test_constructor_datetimes_mixed_tzs(self):
expected = Index([dt1, dt2], dtype=object)
tm.assert_index_equal(result, expected)

def test_constructor_list_between_elems(self):
# #60925 should raise when one of index's items is a list and others are not
msg = "Index names must all be hashable, or all lists to make MultiIndex"

data = ['a', ['b', 'c'], ['b', 'c']]
with pytest.raises(ValueError, match=msg):
Index(data)

data = [['b', 'c'], ('b', 'c')]
with pytest.raises(ValueError, match=msg):
Index(data)

class TestDtypeEnforced:
# check we don't silently ignore the dtype keyword
Expand Down
Loading