{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/common.py#L163-L199","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ni = positions.mean()  # float\nval = ser[i]\n// after\ni = int(round(positions.mean()))\nval = ser[i]","handlingStrategy":"validation","validationCode":"if isinstance(i, float) and i.is_integer():\n    i = int(i)\nval = ser[i]","typeGuard":"def is_integer_indexer(i) -> bool:\n    import numpy as np\n    return isinstance(i, (int, np.integer))","tryCatchPattern":null,"preventionTips":["Coerce float indices to int explicitly with int().","Validate isinstance(i, (int, np.integer)) at boundaries.","Avoid numpy float results as direct indices."],"tags":["index-error","float-indexer","indexing","scalar","deprecated"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}