Skip to content

Commit 3781d86

Browse files
committed
DEPR: Disallow indexing an Index with a float
1 parent eb69d89 commit 3781d86

File tree

5 files changed

+12
-20
lines changed

5 files changed

+12
-20
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Removal of prior version deprecations/changes
275275
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
276276
- Renamed ``fname`` to ``path`` in :meth:`DataFrame.to_parquet`, :meth:`DataFrame.to_stata` and :meth:`DataFrame.to_feather` (:issue:`30338`)
277277
- Enforced disallowing indexing a :class:`Series` with a single item list with a slice (e.g. ``ser[[slice(0, 2)]]``). Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`)
278+
- Enforced disallowing indexing an :class:`Index` object with a float key which will raise an ``IndexError`` (:issue:`34191`).
278279
- Enforced the ``display.max_colwidth`` option to not accept negative integers (:issue:`31569`)
279280
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
280281
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)

pandas/core/common.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,24 @@ def is_bool_indexer(key: Any) -> bool:
148148
return False
149149

150150

151-
def cast_scalar_indexer(val, warn_float: bool = False):
151+
def cast_scalar_indexer(val):
152152
"""
153153
To avoid numpy DeprecationWarnings, cast float to integer where valid.
154154
155155
Parameters
156156
----------
157157
val : scalar
158-
warn_float : bool, default False
159-
If True, issue deprecation warning for a float indexer.
160158
161159
Returns
162160
-------
163161
outval : scalar
164162
"""
165163
# assumes lib.is_scalar(val)
166164
if lib.is_float(val) and val.is_integer():
167-
if warn_float:
168-
warnings.warn(
169-
"Indexing with a float is deprecated, and will raise an IndexError "
170-
"in pandas 2.0. You can manually convert to an integer key instead.",
171-
FutureWarning,
172-
stacklevel=find_stack_level(),
173-
)
174-
return int(val)
165+
raise IndexError(
166+
"Indexing with a float is not allowed. Manually convert "
167+
f"to {val} to an integer key instead.",
168+
)
175169
return val
176170

177171

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5221,7 +5221,7 @@ def __getitem__(self, key):
52215221

52225222
if is_integer(key) or is_float(key):
52235223
# GH#44051 exclude bool, which would return a 2d ndarray
5224-
key = com.cast_scalar_indexer(key, warn_float=True)
5224+
key = com.cast_scalar_indexer(key)
52255225
return getitem(key)
52265226

52275227
if isinstance(key, slice):

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ def __reduce__(self):
20312031

20322032
def __getitem__(self, key):
20332033
if is_scalar(key):
2034-
key = com.cast_scalar_indexer(key, warn_float=True)
2034+
key = com.cast_scalar_indexer(key)
20352035

20362036
retval = []
20372037
for lev, level_codes in zip(self.levels, self.codes):

pandas/tests/indexes/test_indexing.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,11 @@ def test_putmask_with_wrong_mask(self, index):
287287
@pytest.mark.parametrize(
288288
"idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])]
289289
)
290-
def test_getitem_deprecated_float(idx):
291-
# https://github.com/pandas-dev/pandas/issues/34191
290+
def test_getitem_raises_float(idx):
291+
# https://github.com/pandas-dev/pandas/issues/34191; enforced 2.0
292292

293-
with tm.assert_produces_warning(FutureWarning):
294-
result = idx[1.0]
295-
296-
expected = idx[1]
297-
assert result == expected
293+
with pytest.raises(IndexError, match="Indexing with a float is not allowed"):
294+
idx[1.0]
298295

299296

300297
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)