pandas-dev/pandas · error · IndexError
Indexing with a float is no longer supported. Manually…
Error message
Indexing with a float is no longer supported. Manually convert to an integer key instead.
What it means
Raised by cast_scalar_indexer when a scalar float that satisfies val.is_integer() (e.g. 1.0, 2.0) is used as a positional/label indexer. Pandas deprecated float indexing in GH#34193 because 1.0 == 1 caused silent confusion between label and positional access; now it is a hard IndexError. The check runs only on scalars that pass lib.is_scalar.
Solutions
- Convert with int(): ser[int(idx)].
- Compute the index with integer arithmetic: i = i // 1 if you want floor.
- Validate isinstance(idx, (int, np.integer)) before indexing.
Example fix
// before i = positions.mean() # float val = ser[i] // after i = int(round(positions.mean())) val = ser[i]
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(i, float) and i.is_integer():
i = int(i)
val = ser[i] Type guard
def is_integer_indexer(i) -> bool:
import numpy as np
return isinstance(i, (int, np.integer)) Prevention
- Coerce float indices to int explicitly with int().
- Validate isinstance(i, (int, np.integer)) at boundaries.
- Avoid numpy float results as direct indices.
When it happens
Trigger: ser[1.0]; df.iloc[2.0]; ser.loc[3.0] on an integer index; idx = some_float; ser[idx] where idx happens to be a whole float.
Common situations: Math that produces float results fed directly as an index (np.mean, division, math.ceil returns int but np.divide returns float); JSON-parsed values that arrive as float; numba/numpy interop where ints are widened to float64.
Related errors
- only integers, slices (`:`), ellipsis (`...`)…
- can only insert Interval objects and NA into an…
- Cannot construct from scalar data. Pass a sequence instead.
- cannot evaluate scalar only bool ops
- Cannot mask with non-boolean array containing NA / NaN…
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/21ac28d2a9593c94.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/common.py:181
return False
def cast_scalar_indexer(val: Any) -> Any:
"""
Disallow indexing with a float key, even if that key is a round number.
Parameters
----------
val : scalar
Returns
-------
outval : scalar
"""
# assumes lib.is_scalar(val)
if lib.is_float(val) and val.is_integer():
raise IndexError(
# GH#34193
"Indexing with a float is no longer supported. Manually convert "
"to an integer key instead."
)
return val
def not_none(*args: object) -> Generator[object]:
"""
Returns a generator consisting of the arguments that are not None.
"""
return (arg for arg in args if arg is not None)
def any_none(*args: object) -> bool:
"""
Returns a boolean indicating if any argument is None.
"""View on GitHub (pinned to 3b7651241d)