BUG: Return KeyError for invalid string key
State: closed

- [x] closes #22803
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

In principle, it is nice to raise different errors if the string is invalid, or if the resulting period is missing. However, we want, and need, methods such as ``.loc`` to return errors of predictable types.
And indeed, this is how ``PeriodIndex``, for instance, works:

``` python
In [2]: pd.date_range('2010-10-10', '2010-10-11').get_loc('pippo')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
```
[...]
``` python
/home/nobackup/repo/pandas/pandas/core/indexes/datetimes.py in get_loc(self, key, method, tolerance)
   1019                 if 'list-like' in str(e):
   1020                     raise e
-> 1021                 raise KeyError(key)
   1022 
   1023     def _maybe_cast_slice_bound(self, label, side, kind):

KeyError: 'pippo'

```

Labels:
name: Indexing
description: Related to indexing on series/frames, not to indexes themselves
name: Period
description: Period data type

Comments:
Hello @toobaz! Thanks for submitting the PR.

 - There are no PEP8 issues in the file [`pandas/core/indexes/period.py`](https://github.com/toobaz/pandas/blob/fae88204d054916d1bab23c2a4d1c4138bdbc42b/pandas/core/indexes/period.py) !

 - There are no PEP8 issues in the file [`pandas/tests/indexes/period/test_indexing.py`](https://github.com/toobaz/pandas/blob/fae88204d054916d1bab23c2a4d1c4138bdbc42b/pandas/tests/indexes/period/test_indexing.py) !


@Licht-T
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23540?src=pr&el=h1) Report
> Merging [#23540](https://codecov.io/gh/pandas-dev/pandas/pull/23540?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/528527b20f085524f484fef70dcde3c0b051782b?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/23540/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/23540?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #23540      +/-   ##
==========================================
+ Coverage   92.25%   92.25%   +<.01%     
==========================================
  Files         161      161              
  Lines       51207    51212       +5     
==========================================
+ Hits        47239    47244       +5     
  Misses       3968     3968
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.63% <100%> (ø)` | :arrow_up: |
| #single | `42.28% <14.28%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/23540?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/period.py](https://codecov.io/gh/pandas-dev/pandas/pull/23540/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9wZXJpb2QucHk=) | `92.25% <100%> (+0.08%)` | :arrow_up: |

------

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

lgtm.
Thanks!