Skip to content

Error when using .loc[integer] with object Index (also) containing floats #19456

Closed
@toobaz

Description

@toobaz

Code Sample, a copy-pastable example if possible

In [2]: s = pd.Series(range(2), pd.Index([1, 2.], dtype=object))

In [3]: s.loc[1]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-a7c868f8fb38> in <module>()
----> 1 s.loc[1]

/home/nobackup/repo/pandas/pandas/core/indexing.py in __getitem__(self, key)
   1367 
   1368             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1369             return self._getitem_axis(maybe_callable, axis=axis)
   1370 
   1371     def _is_scalar_access(self, key):

/home/nobackup/repo/pandas/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1620 
   1621         # fall thru to straight lookup
-> 1622         self._has_valid_type(key, axis)
   1623         return self._get_label(key, axis=axis)
   1624 

/home/nobackup/repo/pandas/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1498 
   1499             try:
-> 1500                 key = self._convert_scalar_indexer(key, axis)
   1501                 if not ax.contains(key):
   1502                     error()

/home/nobackup/repo/pandas/pandas/core/indexing.py in _convert_scalar_indexer(self, key, axis)
    247         ax = self.obj._get_axis(min(axis, self.ndim - 1))
    248         # a scalar
--> 249         return ax._convert_scalar_indexer(key, kind=self.name)
    250 
    251     def _convert_slice_indexer(self, key, axis):

/home/nobackup/repo/pandas/pandas/core/indexes/base.py in _convert_scalar_indexer(self, key, kind)
   1373             elif kind in ['loc'] and is_integer(key):
   1374                 if not self.holds_integer():
-> 1375                     return self._invalid_indexer('label', key)
   1376 
   1377         return key

/home/nobackup/repo/pandas/pandas/core/indexes/base.py in _invalid_indexer(self, form, key)
   1557                         "indexers [{key}] of {kind}".format(
   1558                             form=form, klass=type(self), key=key,
-> 1559                             kind=type(key)))
   1560 
   1561     def get_duplicates(self):

TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [1] of <class 'int'>

In [4]: s.loc[1.]
Out[4]: 0

Problem description

I guess this is closely related to #17286 , which however failed also with float keys (using __getitem__ rather than .loc).

Expected Output

In [3]: s.loc[1]
Out[3]: 0

Output of pd.show_versions()

INSTALLED VERSIONS

commit: d740b65
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-5-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.23.0.dev0+188.gd740b65f8
pytest: 3.2.3
pip: 9.0.1
setuptools: 36.7.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.0dev
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.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions