BUG: Fix KeyError in merge on CategoricalIndex
State: closed

For categorical type indices a `KeyError` is raised when the index level is used during a merge on an index level

Example:

```
import pandas as pd
left = pd.DataFrame(
    {"left_data": [1, 2]},
   index=pd.CategoricalIndex(data=["A", "B"], categories=["A", "B"], name='index_col')
)
right = pd.DataFrame(
      {"right_data": [1.0, 2.0]},
   index=pd.CategoricalIndex(data=["A", "B"], categories=["A", "B"], name='index_col')
)
result = left.merge(right, on=['index_col'])
```

With this fix, the behavior of the test `test_merge_datetime_index(self, klass)` changed, though. IMHO, the behavior in this PR is more consistent since it is the same for all input types but I'm not sure what the actual behavior should be and I couldn't find a section in the documentation explaining this path.

- [ ] closes #xxxx
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Labels:
name: Bug
name: Reshaping
description: Concat, Merge/Join, Stack/Unstack, Explode

Comments:
can u show. short example of what you are trying to do
The following code raises a `KeyError` if the index is of categorical type but is ok for all other types
```
import pandas as pd
left = pd.DataFrame(
    {"left_data": [1, 2]},
   index=pd.CategoricalIndex(data=["A", "B"], categories=["A", "B"], name='index_col')
)
right = pd.DataFrame(
      {"right_data": [1.0, 2.0]},
   index=pd.CategoricalIndex(data=["A", "B"], categories=["A", "B"], name='index_col')
)
result = left.merge(right, on=['index_col'])
```

I updated the title and the description
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/20777?src=pr&el=h1) Report
> Merging [#20777](https://codecov.io/gh/pandas-dev/pandas/pull/20777?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/d3d33524cccee1c4c9c00318e286270b5196fde3?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #20777      +/-   ##
==========================================
+ Coverage   91.81%   91.85%   +0.03%     
==========================================
  Files         153      153              
  Lines       49471    49310     -161     
==========================================
- Hits        45422    45292     -130     
+ Misses       4049     4018      -31
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.24% <100%> (+0.03%)` | :arrow_up: |
| #single | `41.89% <50%> (+0.04%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/20777?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/reshape/merge.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzaGFwZS9tZXJnZS5weQ==) | `94.25% <ø> (ø)` | :arrow_up: |
| [pandas/core/algorithms.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYWxnb3JpdGhtcy5weQ==) | `94.38% <100%> (-0.11%)` | :arrow_down: |
| [pandas/core/indexing.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhpbmcucHk=) | `93.08% <0%> (-0.48%)` | :arrow_down: |
| [pandas/core/strings.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc3RyaW5ncy5weQ==) | `98.32% <0%> (-0.31%)` | :arrow_down: |
| [pandas/core/dtypes/base.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2Jhc2UucHk=) | `91.89% <0%> (-0.22%)` | :arrow_down: |
| [pandas/core/dtypes/cast.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2Nhc3QucHk=) | `87.85% <0%> (-0.21%)` | :arrow_down: |
| [pandas/core/indexes/api.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9hcGkucHk=) | `98.78% <0%> (-0.15%)` | :arrow_down: |
| [pandas/core/series.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc2VyaWVzLnB5) | `93.9% <0%> (-0.13%)` | :arrow_down: |
| [pandas/core/dtypes/missing.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL21pc3NpbmcucHk=) | `92.85% <0%> (-0.09%)` | :arrow_down: |
| [pandas/core/indexes/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZWxpa2UucHk=) | `96.72% <0%> (-0.08%)` | :arrow_down: |
| ... and [31 more](https://codecov.io/gh/pandas-dev/pandas/pull/20777/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/20777?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/20777?src=pr&el=footer). Last update [d3d3352...0664858](https://codecov.io/gh/pandas-dev/pandas/pull/20777?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jreback I fixed the formatting and added a changelog entry for the bug I fixed. Since I didn't know for sure if the other change in behavior is intended I left it out for now. Can add another entry once this is settled
thanks @fjetter nice patch!