{"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":"exception","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/base.py#L665-L701","documentation":"The indexed form of ExtensionArray.item(index) validates that the supplied index is an integer via pandas' is_integer check. Passing a float, string, numpy float64, or any non-integral type raises TypeError because element access by position requires a concrete int. This guards against silent float->int truncation that numpy semantics would otherwise invite.","triggerScenarios":"Calling arr.item(1.0), arr.item(np.float64(2)), arr.item('1'), or arr.item(np.int32(0)) on some platforms/configurations where the value is not recognized as a Python int. Also triggered by passing a value computed via division or averaging that is nominally integral but typed float.","commonSituations":"Index computed from len()/2 or an averaging expression yielding a float. Passing a numpy scalar whose kind is not recognized by is_integer. Pulling an index out of a dict/JSON as a string.","solutions":["Coerce to int explicitly before calling: arr.item(int(idx)).","Compute the index with integer division (//) instead of true division (/).","If the index is symbolic, use arr.loc-style access on a wrapping Series instead of .item(name)."],"exampleFix":"// before\npos = len(arr) / 2\narr.item(pos)  # TypeError\n\n// after\npos = len(arr) // 2\narr.item(int(pos))","handlingStrategy":"validation","validationCode":"from pandas.api.types import is_integer\nif not is_integer(index):\n    raise TypeError(f'index must be int, got {type(index).__name__}')\n_ = arr.item(int(index))","typeGuard":"def is_int_index(i) -> bool:\n    from pandas.api.types import is_integer\n    return is_integer(i)","tryCatchPattern":"try:\n    val = arr.item(idx)\nexcept TypeError:\n    val = arr.item(int(idx))","preventionTips":["Always coerce computed indices with int(...) before .item().","Use integer division // rather than / for index arithmetic.","Pull indices from integer-typed sources, not JSON/dicts which give strings."],"tags":["extension-array","type-error","numpy-compat","pandas"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}