clean CategoricalIndex.get_loc and improve error message
State: closed

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.

```python
>>> 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:

```python
>>> 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?


Labels:
name: Categorical
description: Categorical Data Type

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

 - There are no PEP8 issues in the file [`pandas/core/indexes/category.py`](https://github.com/topper-123/pandas/blob/18714b35f303c621e558e93009ea108e441bdc36/pandas/core/indexes/category.py) !


# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=h1) Report
> Merging [#23091](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c8ce3d01e9ffafc24c6f9dd568cd9eb7e42c610c?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/23091/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=tree)

```diff
@@            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%> (ø)` | :arrow_up: |
| #single | `42.3% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/category.py](https://codecov.io/gh/pandas-dev/pandas/pull/23091/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9jYXRlZ29yeS5weQ==) | `97.54% <100%> (+0.27%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=footer). Last update [c8ce3d0...18714b3](https://codecov.io/gh/pandas-dev/pandas/pull/23091?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

thanks @topper-123