Description
Code Sample, a copy-pastable example if possible
In [2]: pd.Series(-1, index=list('abc')).loc[1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-c0de4e42231c> in <module>()
----> 1 pd.Series(-1, index=list('abc')).loc[1]
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
1476
1477 maybe_callable = com._apply_if_callable(key, self.obj)
-> 1478 return self._getitem_axis(maybe_callable, axis=axis)
1479
1480 def _is_scalar_access(self, key):
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
1909
1910 # fall thru to straight lookup
-> 1911 self._validate_key(key, axis)
1912 return self._get_label(key, axis=axis)
1913
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
1786
1787 try:
-> 1788 key = self._convert_scalar_indexer(key, axis)
1789 if not ax.contains(key):
1790 error()
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _convert_scalar_indexer(self, key, axis)
259 ax = self.obj._get_axis(min(axis, self.ndim - 1))
260 # a scalar
--> 261 return ax._convert_scalar_indexer(key, kind=self.name)
262
263 def _convert_slice_indexer(self, key, axis):
/home/pietro/nobackup/repo/pandas/pandas/core/indexes/base.py in _convert_scalar_indexer(self, key, kind)
1662 elif kind in ['loc'] and is_integer(key):
1663 if not self.holds_integer():
-> 1664 return self._invalid_indexer('label', key)
1665
1666 return key
/home/pietro/nobackup/repo/pandas/pandas/core/indexes/base.py in _invalid_indexer(self, form, key)
1846 "indexers [{key}] of {kind}".format(
1847 form=form, klass=type(self), key=key,
-> 1848 kind=type(key)))
1849
1850 def get_duplicates(self):
TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [1] of <class 'int'>
In [3]: pd.Series(-1, index=list('abc')).loc[1.0]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
1789 if not ax.contains(key):
-> 1790 error()
1791 except TypeError as e:
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in error()
1784 .format(key=key,
-> 1785 axis=self.obj._get_axis_name(axis)))
1786
KeyError: 'the label [1.0] is not in the [index]'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-3-2f3185e45839> in <module>()
----> 1 pd.Series(-1, index=list('abc')).loc[1.0]
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
1476
1477 maybe_callable = com._apply_if_callable(key, self.obj)
-> 1478 return self._getitem_axis(maybe_callable, axis=axis)
1479
1480 def _is_scalar_access(self, key):
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
1909
1910 # fall thru to straight lookup
-> 1911 self._validate_key(key, axis)
1912 return self._get_label(key, axis=axis)
1913
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in _validate_key(self, key, axis)
1796 raise
1797 except:
-> 1798 error()
1799
1800 def _is_scalar_access(self, key):
/home/pietro/nobackup/repo/pandas/pandas/core/indexing.py in error()
1783 raise KeyError(u"the label [{key}] is not in the [{axis}]"
1784 .format(key=key,
-> 1785 axis=self.obj._get_axis_name(axis)))
1786
1787 try:
KeyError: 'the label [1.0] is not in the [index]'
Problem description
1
and 1.0
are invalid keys in exactly the same way. So they should raise the same error (and the wrong error is the ValueError
, because pd.Series(-1, index=['a', 1]).loc[1]
works just fine, which means that it's not a matter of type of key/index).
Closely related to #19456. Also related to #17569.
Expected Output
The same KeyError
.
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
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: en_GB.UTF-8
LOCALE: en_GB.UTF-8
pandas: 0.24.0.dev0+26.gbe90d4928
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.18.1
pyarrow: None
xarray: None
IPython: 5.2.2
sphinx: None
patsy: 0.4.1+dev
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: None
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None