Skip to content
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

Why [] operation on float index doesn't work the same way as int index? #31344

Closed
GYHHAHA opened this issue Jan 27, 2020 · 2 comments
Closed
Labels
Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@GYHHAHA
Copy link
Contributor

GYHHAHA commented Jan 27, 2020

Code Sample, a copy-pastable example if possible

>>> s1 = pd.Series(list('abcde'),index=[5,7,9,11,13])
>>> s2 = pd.Series(list('abcde'),index=[5.5,7.5,9.5,11.5,13.5])
>>> print(s1[2:])
>>> print(s2[2:])
9     c
11    d
13    e
dtype: object
5.5     a
7.5     b
9.5     c
11.5    d
13.5    e
dtype: object

Problem description

I'm a little confused with the [] operation on numerical index.
It seems that when [] applied to the int index, it is compared with the exact position, whereas the [] operation on float is compared with the index's value.
Thanks!

@MarcoGorelli
Copy link
Member

Thanks for the report, seems like it's an open issue (see #7501)

In the meantime, to avoid ambiguity, it's recommended to use either .loc for label-based indexing, or .iloc for position-based indexing

@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Feb 25, 2020
@mroeschke
Copy link
Member

Closing as a duplicate of #7501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants