Skip to content

BUG: fix: list as index item does not raise #61674

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 that aren't (should raise ValueError) (:issue:`60925`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Bug in creating :class:`Index` with one item being a ``list`` among others that aren't (should raise ValueError) (:issue:`60925`)
- Bug in creating :class:`Index` with some items being a non-hashable; this now raises a ``ValueError`` (:issue:`60925`)


Missing
^^^^^^^
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ 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
):
Comment on lines +570 to +573
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No non-hashable value should occur here. Instead, can you do this check on arr below, and only in the case it's (a) a NumPy array (not an ExtensionArray) and (b) of object dtype. Use pandas.core.dtypes.inference.is_hashable.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use GH#60925 throughout.

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)
Comment on lines +78 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should raise on the first line, is that right? If so, can you remove repr(df).


def test_repr_with_mi_nat(self):
df = DataFrame({"X": [1, 2]}, index=[[NaT, Timestamp("20130101")], ["a", "b"]])
Expand Down
12 changes: 12 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,18 @@ 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