Repository: pandas-dev/pandas

Issue:
KeyError error message could be less confusing
#### Code Sample, a copy-pastable example if possible

```python
In [2]: pd.Series(range(3)).loc[4]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1810                 if not ax.contains(key):
-> 1811                     error()
   1812             except TypeError as e:

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 

KeyError: 'the label [4] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-2-a40b2ecc79ee> in <module>()
----> 1 pd.Series(range(3)).loc[4]

/home/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
   1497 
   1498             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1499             return self._getitem_axis(maybe_callable, axis=axis)
   1500 
   1501     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1930 
   1931         # fall thru to straight lookup
-> 1932         self._validate_key(key, axis)
   1933         return self._get_label(key, axis=axis)
   1934 

/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1817                 raise
   1818             except:
-> 1819                 error()
   1820 
   1821     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1804                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 
   1808             try:

KeyError: 'the label [4] is not in the [index]'
```

#### Problem description

The way in which we catch exceptions is uninteresting to the user. The sequence of two exceptions suggests to the user that there is a bug in the code, or unexpected exception, which is not the case; so this error message is confusing.

The fix is trivial.

#### Expected Output
```python
In [2]: pd.Series(range(3)).loc[4]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-a40b2ecc79ee> in <module>()
----> 1 pd.Series(range(3)).loc[4]

/home/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
   1497 
   1498             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1499             return self._getitem_axis(maybe_callable, axis=axis)
   1500 
   1501     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1931 
   1932         # fall thru to straight lookup
-> 1933         self._validate_key(key, axis)
   1934         return self._get_label(key, axis=axis)
   1935 

/home/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
   1818 
   1819             if not ax.contains(key):
-> 1820                 error()
   1821 
   1822     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in error()
   1804                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1805                                .format(key=key,
-> 1806                                        axis=self.obj._get_axis_name(axis)))
   1807 
   1808             try:

KeyError: 'the label [4] is not in the [index]'
```
#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: b36b451a74bc16d7ea64c158a3cd33fbfb504068
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-6-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.24.0.dev0+132.gb36b451a7
pytest: 3.5.0
pip: 9.0.1
setuptools: 39.2.0
Cython: 0.25.2
numpy: 1.14.3
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.0dev
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.2.2.post1153+gff6786446
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.3.0
xlsxwriter: 0.9.6
lxml: 4.1.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1


</details>



Discussion:

Resolving patch:
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -1807,8 +1807,6 @@ def error():
 
             try:
                 key = self._convert_scalar_indexer(key, axis)
-                if not ax.contains(key):
-                    error()
             except TypeError as e:
 
                 # python 3 type errors should be raised
@@ -1818,6 +1816,9 @@ def error():
             except:
                 error()
 
+            if not ax.contains(key):
+                error()
+
     def _is_scalar_access(self, key):
         # this is a shortcut accessor to both .loc and .iloc
         # that provide the equivalent access of .at and .iat


Test patch: