Skip to content

BUG: getitem access with floats and an object index #17286

Closed
@prcastro

Description

@prcastro

Code Sample, a copy-pastable example if possible

>>> counts = pd.Series([1.0, 2.0, 3.0, 'A'], dtype='object').value_counts()
>>> counts
3.0    1
2.0    1
1.0    1
A      1
dtype: int64

>>> counts['A']
1

>>> counts[1.0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-338-ba9f2c20492a> in <module>()
----> 1 counts[1.0]

/disk1/home/synergyds/.anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    599         key = com._apply_if_callable(key, self)
    600         try:
--> 601             result = self.index.get_value(self, key)
    602 
    603             if not is_scalar(result):

/disk1/home/synergyds/.anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   2423         k = _values_from_object(key)
   2424 
-> 2425         k = self._convert_scalar_indexer(k, kind='getitem')
   2426         try:
   2427             return self._engine.get_value(s, k,

/disk1/home/synergyds/.anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in _convert_scalar_indexer(self, key, kind)
   1267             if kind in ['getitem', 'ix'] and is_float(key):
   1268                 if not self.is_floating():
-> 1269                     return self._invalid_indexer('label', key)
   1270 
   1271             elif kind in ['loc'] and is_float(key):

/disk1/home/synergyds/.anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in _invalid_indexer(self, form, key)
   1467                         "indexers [{key}] of {kind}".format(
   1468                             form=form, klass=type(self), key=key,
-> 1469                             kind=type(key)))
   1470 
   1471     def get_duplicates(self):

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

Problem description

If a Series has an index with type object, I can't access a row using a float64 key, even if the key is indeed present in the index.

Expected Output

>>> counts[1.0]
1

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-514.26.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions