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

DEPR: deprecate Index.__getitem__ with float key #34191

Closed
jorisvandenbossche opened this issue May 15, 2020 · 4 comments · Fixed by #34193
Closed

DEPR: deprecate Index.__getitem__ with float key #34191

jorisvandenbossche opened this issue May 15, 2020 · 4 comments · Fixed by #34193
Labels
API - Consistency Internal Consistency of API/Behavior Deprecate Functionality to remove in pandas Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@jorisvandenbossche
Copy link
Member

I would propose to deprecate interpreting float numbers as integers in Index __getitem__.

Numpy does (no longer) allow this:

In [1]: a = np.array([1, 2, 3])   

In [3]: a[0.0]      
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-3-2ec1d2c6d8d4> in <module>
----> 1 a[0.0]

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

With a Series with iloc, we also don't allow this:

In [7]: pd.Series(a).iloc[0.0] 
...
TypeError: Cannot index by location index with a non-integer key

But for Index we do check if the float is integer-like and in that case convert to integer:

In [5]: pd.Index(a)[0.0]  
Out[5]: 1

However, this is not for all Index subclasses the case, eg:

In [9]: pd.DatetimeIndex(a)[0.0]                                                                                                                                                                                   
...
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

So I think we can simply deprecate this to align Index with numpy/Series/some Index subclasses.

@jorisvandenbossche jorisvandenbossche added Deprecate Functionality to remove in pandas API - Consistency Internal Consistency of API/Behavior labels May 15, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.1 milestone May 15, 2020
@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label May 15, 2020
@jbrockmendel
Copy link
Member

Can you specify which Index subclasses and methods would be affected?

In particular, would this address #33469, #31344, #7501

@jorisvandenbossche
Copy link
Member Author

@jbrockmendel all the issue you link to are about Series.__getitem__ with a float/integer index, this is about Index.__getitem__ with a float/integer key.

I had searched for related issues, but didn't directly find any, so opened this one.

This is the case for where com.cast_scalar_indexer is used in the __setitem__ definition, which means the base class Index (and the numeric subclasses that inherit setitem) and MultiIndex, and not for any of the ExtensionIndex subclasses (since that redefines __setitem__ without using com.cast_scalar_indexer)

@jbrockmendel
Copy link
Member

Thanks for clarifying.

@jorisvandenbossche
Copy link
Member Author

There is a PR, if you want to see it in code ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Deprecate Functionality to remove in pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants