Skip to content

Commit aa27dc8

Browse files
BUG #60925 list as index item
1 parent 2096b28 commit aa27dc8

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

pandas/core/indexes/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ 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(isinstance(el, list) for el in data):
572+
raise ValueError("Index names must all be hashable, or all lists to make MultiIndex")
573+
570574
try:
571575
arr = sanitize_array(data, None, dtype=dtype, copy=copy)
572576
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ 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)
191202

192203
class TestDtypeEnforced:
193204
# check we don't silently ignore the dtype keyword

0 commit comments

Comments
 (0)