{"record":{"id":"c2348a99204b3374","repo":"pandas-dev/pandas","slug":"index-must-be-an-integer-got-type-index","errorCode":null,"errorMessage":"index must be an integer, got {type(index)}","messagePattern":"index must be an integer, got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/base.py","lineNumber":683,"sourceCode":"        >>> arr = pd.array([1], dtype=\"Int64\")\n        >>> arr.item()\n        np.int64(1)\n\n        >>> arr = pd.array([1, 2, 3], dtype=\"Int64\")\n        >>> arr.item(0)\n        np.int64(1)\n        >>> arr.item(2)\n        np.int64(3)\n        \"\"\"\n        if index is None:\n            if len(self) != 1:\n                raise ValueError(\n                    \"can only convert an array of size 1 to a Python scalar\"\n                )\n            return self[0]\n        else:\n            if not is_integer(index):\n                raise TypeError(f\"index must be an integer, got {type(index)}\")\n            return self[index]\n\n    def to_numpy(\n        self,\n        dtype: npt.DTypeLike | None = None,\n        copy: bool = False,\n        na_value: object = lib.no_default,\n    ) -> np.ndarray:\n        \"\"\"\n        Convert to a NumPy ndarray.\n\n        This is similar to :meth:`numpy.asarray`, but may provide additional control\n        over how the conversion is done.\n\n        Parameters\n        ----------\n        dtype : str or numpy.dtype, optional\n            The dtype to pass to :meth:`numpy.asarray`.","sourceCodeStart":665,"sourceCodeEnd":701,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/base.py#L665-L701","documentation":"Raised by ExtensionArray.item(index) when the provided `index` is not an integer (e.g. a string, float, numpy float64, slice, or None passed positionally as something other than None). The method uses pandas.core.dtypes.common.is_integer to validate; only Python ints (and numpy integer scalars) pass. Reached via pd.array(...).item(index) or Series.array.item(index).","triggerScenarios":"Calling `arr.item(0.0)`, `arr.item(\"0\")`, or `arr.item(np.float64(2))`. Also when a slice or list is mistakenly passed to item() instead of an integer index.","commonSituations":"Index values coming from JSON/CSV as strings or floats; numpy operations returning float64 indices; dynamic indexing code that loses int typing.","solutions":["Coerce to int: `arr.item(int(index))`.","Use is_integer from pandas to validate before calling: `from pandas.api.types import is_integer`.","For slices/lists use `arr[key]` directly instead of item().","Sanitize upstream so indices are Python ints."],"exampleFix":"# before\nidx = np.float64(2)\narr.item(idx)  # TypeError: index must be an integer, got float64\n\n# after\narr.item(int(idx))","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_integer\n\ndef safe_item(arr, index):\n    if not is_integer(index):\n        raise TypeError(f\"index must be int, got {type(index).__name__}\")\n    return arr.item(int(index))","typeGuard":"import numbers\nfrom pandas.api.types import is_integer\n\ndef is_valid_index(v) -> bool:\n    return is_integer(v) or isinstance(v, numbers.Integral)","tryCatchPattern":"try:\n    return arr.item(index)\nexcept TypeError as e:\n    if \"index must be an integer\" in str(e):\n        return arr.item(int(index))\n    raise","preventionTips":["Coerce indices to Python int before calling item().","Sanitize JSON/CSV-derived indices at load time.","Use arr[key] for slices/lists instead of item()."],"tags":["extension-array","type-error","indexing"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}