{"record":{"id":"21ac28d2a9593c94","repo":"pandas-dev/pandas","slug":"indexing-with-a-float-is-no-longer-supported-manu","errorCode":null,"errorMessage":"Indexing with a float is no longer supported. Manually convert to an integer key instead.","messagePattern":"Indexing with a float is no longer supported\\. Manually convert to an integer key instead\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"pandas/core/common.py","lineNumber":181,"sourceCode":"\n    return False\n\n\ndef cast_scalar_indexer(val: Any) -> Any:\n    \"\"\"\n    Disallow indexing with a float key, even if that key is a round number.\n\n    Parameters\n    ----------\n    val : scalar\n\n    Returns\n    -------\n    outval : scalar\n    \"\"\"\n    # assumes lib.is_scalar(val)\n    if lib.is_float(val) and val.is_integer():\n        raise IndexError(\n            # GH#34193\n            \"Indexing with a float is no longer supported. Manually convert \"\n            \"to an integer key instead.\"\n        )\n    return val\n\n\ndef not_none(*args: object) -> Generator[object]:\n    \"\"\"\n    Returns a generator consisting of the arguments that are not None.\n    \"\"\"\n    return (arg for arg in args if arg is not None)\n\n\ndef any_none(*args: object) -> bool:\n    \"\"\"\n    Returns a boolean indicating if any argument is None.\n    \"\"\"","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/common.py#L163-L199","documentation":"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.","triggerScenarios":"`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)).","commonSituations":"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.","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."],"exampleFix":"# before\nkey = n / step      # float\ns[key]\n\n# after\nkey = n // step     # int\ns[key]","handlingStrategy":"validation","validationCode":"def as_int_key(key):\n    if isinstance(key, float) and key.is_integer():\n        return int(key)\n    return key\n\ns[as_int_key(key)]","typeGuard":"def is_safe_indexer(key) -> bool:\n    import numbers\n    if isinstance(key, float):\n        return not key.is_integer()\n    return True","tryCatchPattern":"try:\n    val = s[key]\nexcept IndexError as e:\n    if 'float' in str(e):\n        val = s[int(key)]\n    else:\n        raise","preventionTips":["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."],"tags":["indexing","float-key","cast","indexerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}