Skip to content

Commit 1843ea5

Browse files
bug #60925 fix
1 parent 5909621 commit 1843ea5

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ Indexing
749749
- Bug in :meth:`MultiIndex.insert` when a new value inserted to a datetime-like level gets cast to ``NaT`` and fails indexing (:issue:`60388`)
750750
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
751751
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
752+
- Bug in creating :class:`Index` with one item being a ``list`` among others that aren't (should raise ValueError) (:issue:`60925`)
752753

753754
Missing
754755
^^^^^^^

pandas/core/indexes/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ def __new__(
567567
# Ensure we get 1-D array of tuples instead of 2D array.
568568
data = com.asarray_tuplesafe(data, dtype=_dtype_obj)
569569

570+
# 60925 should raise when one of index's items is a list and others are not
571+
if any(isinstance(el, list) for el in data) and not all(
572+
isinstance(el, list) for el in data
573+
):
574+
raise ValueError(
575+
"Index names must all be hashable, or all lists to make MultiIndex"
576+
)
577+
570578
try:
571579
arr = sanitize_array(data, None, dtype=dtype, copy=copy)
572580
except ValueError as err:

pandas/tests/frame/test_repr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ def test_assign_index_sequences(self):
7171
repr(df)
7272

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

7881
def test_repr_with_mi_nat(self):
7982
df = DataFrame({"X": [1, 2]}, index=[[NaT, Timestamp("20130101")], ["a", "b"]])

pandas/tests/indexes/test_index_new.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ def test_constructor_datetimes_mixed_tzs(self):
188188
expected = Index([dt1, dt2], dtype=object)
189189
tm.assert_index_equal(result, expected)
190190

191+
def test_constructor_list_between_elems(self):
192+
# 60925 should raise when one of index's items is a list and others are not
193+
msg = "Index names must all be hashable, or all lists to make MultiIndex"
194+
195+
data = ["a", ["b", "c"], ["b", "c"]]
196+
with pytest.raises(ValueError, match=msg):
197+
Index(data)
198+
199+
data = [["b", "c"], ("b", "c")]
200+
with pytest.raises(ValueError, match=msg):
201+
Index(data)
202+
191203

192204
class TestDtypeEnforced:
193205
# check we don't silently ignore the dtype keyword

0 commit comments

Comments
 (0)