pandas-dev/pandas · error · IndexError
Indexing with a float is no longer supported. Manually conve
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 (pandas/core/common.py:181) when a scalar float that is a whole number (e.g. 3.0) is used as an index key. Historically pandas allowed 3.0 to be coerced to 3, which hid indexing bugs and was inconsistent with Python's strict int/float distinction; since the deprecation finalized (GH#34193) float keys are rejected.
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 71959b8cb9)
Solutions
- Convert the key to int explicitly: `s[int(3.0)]` or `s[int(key)]`.
- Fix the arithmetic producing the float: use integer division `//` or `math.ceil`/`floor`.
- If the index genuinely holds float labels, index with the exact float that is NOT a whole number, or cast the index to int/str.
Example fix
# before key = n / step # float s[key] # after key = n // step # int s[key]
Defensive patterns
Strategy: validation
Validate before calling
def as_int_key(key):
if isinstance(key, float) and key.is_integer():
return int(key)
return key
s[as_int_key(key)] Type guard
def is_safe_indexer(key) -> bool:
import numbers
if isinstance(key, float):
return not key.is_integer()
return True Try / catch
try:
val = s[key]
except IndexError as e:
if 'float' in str(e):
val = s[int(key)]
else:
raise Prevention
- Convert keys to int before indexing: s[int(key)].
- Use integer division // instead of / for index arithmetic.
- Validate external/config keys with int() before use.
When it happens
Trigger: `s[3.0]`, `df.loc[3.0]`, `df.iloc[3.0]`, or `df[3.0]` where the float is a round integer. Also triggered when a key comes from division (`i / 1`) or numpy float scalars (np.float64(2.0)).
Common situations: Keys produced by arithmetic that yields float (e.g. `len//n` vs `len/n`), JSON/config values parsed as float, numpy float64 scalars from reductions, or code migrated from older pandas that silently coerced.
Related errors
- only integers, slices (`:`), ellipsis (`...`), numpy.newaxis
- key must be an int or slice, got {type(key).__name__}
- name_or_index must be an int, str, bytes, pyarrow.compute.Ex
- Only integers, slices and integer or boolean arrays are vali
- only integers, slices (`:`), ellipsis (`...`), numpy.newaxis
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/21ac28d2a9593c94.
Report an issue: GitHub.