Skip to content

clean CategoricalIndex.get_loc and improve error message #23091

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

Merged

Conversation

topper-123
Copy link
Contributor

@topper-123 topper-123 commented Oct 11, 2018

This is an offspring from #21699 to do the a cleanup in a contained PR.

self.categories.get_loc(key) can never return -1, so the if (codes == -1): raise KeyError(key) is unnecessary. Instead, if key is not in self.categories, a KeyError is raised straight away.

>>> ci = pd.CategoricalIndex(['a', 'b'], categories=['a', 'b', 'c'])
>>> ci.get_loc('d')
KeyError: 'd'  # both master and this PR

A slightly confusing issue is that if the key is found in categories, but the code is not found in self._engine, the code is shown in the KeyError message. It would probably be more intuitive that the key would be shown:

>>> ci.get_loc('c')
KeyError: 2  # master, confusing IMO
KeyError: 'c'  # this PR

As the error type is unchanged, does this change warrant a whatsnew entry?

@pep8speaks
Copy link

Hello @topper-123! Thanks for submitting the PR.

@codecov
Copy link

codecov bot commented Oct 11, 2018

Codecov Report

Merging #23091 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #23091      +/-   ##
==========================================
+ Coverage    92.2%    92.2%   +<.01%     
==========================================
  Files         169      169              
  Lines       50924    50925       +1     
==========================================
+ Hits        46952    46954       +2     
+ Misses       3972     3971       -1
Flag Coverage Δ
#multiple 90.62% <100%> (ø) ⬆️
#single 42.3% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/category.py 97.54% <100%> (+0.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c8ce3d0...18714b3. Read the comment docs.

@jreback jreback added the Categorical Categorical Data Type label Oct 18, 2018
@jreback jreback added this to the 0.24.0 milestone Oct 18, 2018
@jreback jreback merged commit 5e06c84 into pandas-dev:master Oct 18, 2018
@jreback
Copy link
Contributor

jreback commented Oct 18, 2018

thanks @topper-123

@topper-123 topper-123 deleted the clean_CategoricalIndex.get_loc branch October 18, 2018 17:43
tm9k1 pushed a commit to tm9k1/pandas that referenced this pull request Nov 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants