Open
Description
MultiIndex
es with empty levels are valid, but set_levels
does not account for that case:
index = pd.MultiIndex(levels=[[], []], labels=[[], []], names=['a', 'b'])
index.set_levels([], level='a')
The set_levels
code that looks like
if is_list_like(levels[0]):
raise TypeError("Levels must be list-like")
should probably be
if len(levels) > 0 and is_list_like(levels[0]):
raise TypeError("Levels must be list-like")
While I'm looking at this, a couple other questiosn:
- How come we only check the first element? What if levels looks like
['a', ['b']]
? I think we need to doany(is_list_like(x) for x in levels)
, which has the additional benefit that empty levels are handled correctly. - Can we make the
TypeError
message more precise? Maybe something likeValueError("Levels must not contain list-like items")