Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
prcastro opened this issue Aug 18, 2017 · 3 comments · Fixed by #41674
Closed

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

prcastro opened this issue Aug 18, 2017 · 3 comments · Fixed by #41674
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@prcastro
Copy link
Contributor

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

@prcastro prcastro changed the title Accessing index of type object with float key raises an error, even if key is present in index Accessing series index of type "object" using float key raises an error, even if that key is present in the index Aug 18, 2017
@gfyoung gfyoung added the Indexing Related to indexing on series/frames, not to indexes themselves label Aug 18, 2017
@gfyoung
Copy link
Member

gfyoung commented Aug 18, 2017

@prcastro : Thanks for reporting this! This seems pretty reasonable, and PR is welcome!

@jreback
Copy link
Contributor

jreback commented Aug 18, 2017

[] has some very subtle differences w.r.t. indexing because it cannot know whether you actually mean label or positional indexing.

In [5]: counts.loc[1.0]
Out[5]: 1

works and is unambiguous.

so I suppose this is a bug.

@jreback jreback added this to the Next Major Release milestone Aug 18, 2017
@jreback jreback changed the title Accessing series index of type "object" using float key raises an error, even if that key is present in the index BUG: getitem access with floats and an object index Aug 18, 2017
@phofl
Copy link
Member

phofl commented Nov 14, 2020

This works now. Needs tests

@phofl phofl added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Nov 14, 2020
@mroeschke mroeschke mentioned this issue May 26, 2021
8 tasks
@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants