Skip to content

Commit fbd20f5

Browse files
committed
Raise error when creating index of tuples with name parameter a string
1 parent f3a7a21 commit fbd20f5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pandas/indexes/multi.py

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
is_object_dtype,
2121
is_iterator,
2222
is_list_like,
23+
is_string_like,
2324
is_scalar)
2425
from pandas.types.missing import isnull, array_equivalent
2526
from pandas.core.common import (_values_from_object,
@@ -490,6 +491,11 @@ def _set_names(self, names, level=None, validate=True):
490491
that it only acts on copies
491492
"""
492493

494+
# GH 15110
495+
# Don't allow a single string for names in a MultiIndex
496+
if names is not None and is_string_like(names):
497+
raise ValueError('Names should not be a single string for a '
498+
'MultiIndex.')
493499
names = list(names)
494500

495501
if validate and level is not None and len(names) != len(level):

pandas/tests/indexes/test_multi.py

+9
Original file line numberDiff line numberDiff line change
@@ -2554,3 +2554,12 @@ def test_unsortedindex(self):
25542554

25552555
with assertRaises(KeyError):
25562556
df.loc(axis=0)['q', :]
2557+
2558+
def test_tuples_with_name_string(self):
2559+
# GH 15110 and GH 14848
2560+
2561+
li = [(0, 0, 1), (0, 1, 0), (1, 0, 0)]
2562+
with assertRaises(ValueError):
2563+
pd.Index(li, name='abc')
2564+
with assertRaises(ValueError):
2565+
pd.Index(li, name='a')

0 commit comments

Comments
 (0)