Repository navigation
Fix nonempty_index for CategoricalIndex - #3394
Conversation
126d1e2 to
125382b
Compare
|
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. |
| 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] |
There was a problem hiding this comment.
It seems like there are two conflicting constraints in the nonempty_index for CategoricalIndex
- A desire to use the real categories for the
.categories - 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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.categoriesDoes that work?
There was a problem hiding this comment.
- 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. Thedatatypes are not tested). Another alternative which preserves the type ofdatawould be
if len(idx.categories) == 0:
data = _nonempty_index(idx.categories)
cats = None
else:
data = _nonempty_index(_nonempty_index(idx.categories))
cats = idx.categories- Both solutions ignore the special case of
__UNKNOWN_CATEGORIES__which ends up asCategoricalIndex([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')] |
There was a problem hiding this comment.
I think this has to be data=[], categories[]` for it to fail without your fix.
There was a problem hiding this comment.
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
125382b to
0421127
Compare
|
Checking in here. What is the status of this? |
|
Sorry for the delay, this looks good. Skipping the release note since this is for compatibility with an unreleased pandas. Thanks @fjetter. |
The
MultiIndexraises aValueErrorin case the data of theCategoricalIndexis not unique (pandas master)ValueError: Level values must be unique: ['b', 'b'] on level 1flake8 daskdocs/source/changelog.rstfor all changesand one of the
docs/source/*-api.rstfiles for new API