Skip to content

Fix nonempty_index for CategoricalIndex - #3394

Merged
TomAugspurger merged 2 commits into
dask:masterfrom
fjetter:bugfix/nonempty_index_categoricals
Apr 19, 2018
Merged

TomAugspurger merged 2 commits into
dask:masterfrom
fjetter:bugfix/nonempty_index_categoricals

Conversation

@fjetter

@fjetter fjetter commented Apr 11, 2018 •

Copy link
Copy Markdown
Member

The MultiIndex raises a ValueError in case the data of the CategoricalIndex is not unique (pandas master)

ValueError: Level values must be unique: ['b', 'b'] on level 1

  • Tests added
  • Tests passed
  • Passes flake8 dask
  • Fully documented, including docs/source/changelog.rst for all changes
    and one of the docs/source/*-api.rst files for new API

@fjetter
fjetter force-pushed the bugfix/nonempty_index_categoricals branch from 126d1e2 to 125382b Compare April 11, 2018 12:05
@mrocklin

Copy link
Copy Markdown
Member

Thank you for the PR @fjetter . My guess is that @TomAugspurger is the right person to review this if he has time. I believe that he may be busy with vacation / pandas release for the next few days though, so there may be a slow response on this.

Comment thread dask/dataframe/utils.py
return pd.CategoricalIndex(data, categories=cats,
ordered=idx.ordered, name=idx.name)
elif typ is pd.MultiIndex:
levels = [_nonempty_index(i) for i in idx.levels]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there are two conflicting constraints in the nonempty_index for CategoricalIndex

  1. A desire to use the real categories for the .categories
  2. A desire for every non_empty index to be length 2.

If given a length-0 or length-1 CategoricalIndex, we can't satisfy both at once. I'd like to solve this issue there, instead of in the MultiIndex part, but that may not be possible. If not, then this approach seems fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to fix it for the CategoricalIndex first but ran into the issues you mentioned already. I was also a bit of confused why the MultiIndex requires unique data in the first place and wasn't sure if this is a pandas issue. From your comment, I guess this is intended behavior?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to link to the pandas issue, pandas-dev/pandas#17464. This was a deliberate change by pandas. Levels of a MI should be unique.

How about this...

         if len(idx.categories) == 0:
             data = _nonempty_index(idx.categories)
             cats = None
         elif len(idx.categories) == 1:
             data = _nonempty_index([idx.categories[0], None])
             cats = idx.categories
         else:
             data = idx.categories[:2]
             cats = idx.categories

Does that work?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I have to pass the list as an index, e.g. _nonempty_index(pd.Index([idx.categories[0], None])) for the second case to work out. However, this would ignore the type of the input (not sure if it matters, though. The data types are not tested). Another alternative which preserves the type of data would be
        if len(idx.categories) == 0:
            data = _nonempty_index(idx.categories)
            cats = None
        else:
            data = _nonempty_index(_nonempty_index(idx.categories))
            cats = idx.categories
  1. Both solutions ignore the special case of __UNKNOWN_CATEGORIES__ which ends up as CategoricalIndex([nan, nan], categories=['__UNKNOWN_CATEGORIES__'], ordered=True, name='foo', dtype='category'). I assume it is pandas setting the data purposely to nan for unknown categories and this is intended behavior, isn't it?

assert res.names == idx.names

levels = [pd.Int64Index([1], name='a'),
pd.CategoricalIndex(data=['b'], categories=['b'], name='b')]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this has to be data=[], categories[]` for it to fail without your fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without my fix this test fails with ValueError: Level values must be unique: ['b', 'b'] on level 1 since the data is duplicated in line https://github.com/fjetter/dask/blob/0421127c6942f4dd904b2c2badfa5fcf3302a5b7/dask/dataframe/utils.py#L338

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're correct.

@fjetter
fjetter force-pushed the bugfix/nonempty_index_categoricals branch from 125382b to 0421127 Compare April 15, 2018 12:29
@mrocklin

Copy link
Copy Markdown
Member

Checking in here. What is the status of this?

@TomAugspurger

Copy link
Copy Markdown
Member

Sorry for the delay, this looks good. Skipping the release note since this is for compatibility with an unreleased pandas.

Thanks @fjetter.

@TomAugspurger
TomAugspurger merged commit 5199320 into dask:master Apr 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants