DEPR: deprecate Index.getitem with float key · Issue #34191 · pandas-dev/pandas (original) (raw)
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.