Skip to content
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

BUG: CategoricalIndex allowed reindexing duplicate sources #28257

Merged
merged 23 commits into from
Oct 16, 2019

Conversation

batterseapower
Copy link
Contributor

For consistency with normal indexes, CategoricalIndex.reindex should allow targets to be duplicated but not the sources. However, currently in allows the source to be duplicated but not the targets, which is exactly the wrong behaviour.

Most of the work here is fixing the tests, which in many cases explicitly check for the incorrect behaviour.

Fixes #25459

(I can't run the full testsuite on my machine but the relevant categorical tests do seem to pass. Hoping that the GitHub CI infrastructure will pick up any other failures.)

@pep8speaks
Copy link

pep8speaks commented Sep 3, 2019

Hello @batterseapower! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-10-11 09:47:23 UTC

@batterseapower
Copy link
Contributor Author

batterseapower commented Sep 3, 2019

Had to fix a couple of incidental Pandas bugs that were surfaced by the main fix.

  1. pd.Index([1, 1, 0, 2, 2]).union(pd.Index([0, 2, -1])) would fail with an error about duplicates in the index. We now return pd.Index([-1, ,0 1, 1, 2, 2]) (or pd.Index([1, 1, 0, 2, 2, -1]) if sort=False) which is more consistent with the behaviour of Index.intersection

  2. pd.Index(['A', 'B']).get_indexer_non_unique(pd.Index([0])) would fail, complaining with a TypeError about '<' not supported between instances of 'str' and 'int'. This is caused by over-aggressive use of searchsorted, solved by falling back to linear search

doc/source/whatsnew/v1.0.0.rst Outdated Show resolved Hide resolved
pandas/_libs/index.pyx Show resolved Hide resolved
pandas/core/indexes/base.py Show resolved Hide resolved
pandas/tests/indexing/test_categorical.py Outdated Show resolved Hide resolved
pandas/tests/indexing/test_categorical.py Show resolved Hide resolved
pandas/tests/series/test_operators.py Outdated Show resolved Hide resolved
pandas/tests/test_base.py Show resolved Hide resolved
@jreback jreback added Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves labels Sep 4, 2019
doc/source/user_guide/advanced.rst Outdated Show resolved Hide resolved
doc/source/whatsnew/v1.0.0.rst Outdated Show resolved Hide resolved
doc/source/whatsnew/v1.0.0.rst Show resolved Hide resolved
Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Looking good overall.

doc/source/whatsnew/v1.0.0.rst Outdated Show resolved Hide resolved
doc/source/whatsnew/v1.0.0.rst Outdated Show resolved Hide resolved
pandas/util/testing.py Show resolved Hide resolved
doc/source/user_guide/advanced.rst Show resolved Hide resolved
pandas/tests/indexes/test_category.py Outdated Show resolved Hide resolved
pandas/tests/series/test_operators.py Show resolved Hide resolved
@batterseapower
Copy link
Contributor Author

Please let me know if this needs more work..

@jreback jreback added this to the 1.0 milestone Oct 11, 2019
@jreback
Copy link
Contributor

jreback commented Oct 11, 2019

lgtm. cc @jbrockmendel @jschendel if any comments.

@jbrockmendel
Copy link
Member

LGTM

@jreback jreback merged commit 86e187f into pandas-dev:master Oct 16, 2019
@jreback
Copy link
Contributor

jreback commented Oct 16, 2019

thanks @batterseapower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CategoricalIndex.reindex raises on duplicate indexer
5 participants