Skip to content

MultiIndex.set_levels and empty levels #16147

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
llchan opened this issue Apr 26, 2017 · 1 comment
Open

MultiIndex.set_levels and empty levels #16147

llchan opened this issue Apr 26, 2017 · 1 comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas MultiIndex

Comments

@llchan
Copy link

llchan commented Apr 26, 2017

MultiIndexes 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 do any(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 like ValueError("Levels must not contain list-like items")
@jreback
Copy link
Contributor

jreback commented Apr 27, 2017

this is related to #14794 (and answers your first question).

Can we make the TypeError message more precise?

yes

@jreback jreback added Bug MultiIndex Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Apr 27, 2017
@jreback jreback added this to the Next Major Release milestone Apr 27, 2017
leej3 added a commit to leej3/pandas that referenced this issue Jul 16, 2017
leej3 added a commit to leej3/pandas that referenced this issue Jul 16, 2017
leej3 added a commit to leej3/pandas that referenced this issue Sep 7, 2017
leej3 added a commit to leej3/pandas that referenced this issue Sep 7, 2017
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants