{"record":{"id":"3aeb83773f1172dc","repo":"pandas-dev/pandas","slug":"can-only-convert-an-array-of-size-1-to-a-python-sc","errorCode":null,"errorMessage":"can only convert an array of size 1 to a Python scalar","messagePattern":"can only convert an array of size 1 to a Python scalar","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/base.py","lineNumber":677,"sourceCode":"        See Also\n        --------\n        numpy.ndarray.item : Return the item of an array as a scalar.\n\n        Examples\n        --------\n        >>> 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","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/base.py#L659-L695","documentation":"ExtensionArray.item() mirrors numpy.ndarray.item(): when called with no arguments it must return the single Python scalar held in a length-1 array. If len(self) != 1 the call is ambiguous (which element?), so pandas raises ValueError. This is a deliberate numpy-compatible contract, not a bug.","triggerScenarios":"Calling arr.item() (no index) on an ExtensionArray of length 0 or length >= 2. Common after a reduction or filter that the caller assumed collapsed to one element but did not, e.g. arr[arr > 0].item() when multiple values match.","commonSituations":"Chaining .item() after a boolean filter expecting a unique hit. Calling .item() on an empty result from a query. Confusing .item() with .item(0) (the indexed form, which does not require length 1).","solutions":["Verify length first: if len(arr) == 1: arr.item() — or just index explicitly with arr.item(0)/arr[0].","If you expect exactly one element, guard with assert len(arr) == 1 before calling .item().","If multiple elements are valid, iterate or use arr.tolist() / arr[0] instead of .item()."],"exampleFix":"// before\nval = arr.item()  # ValueError if len != 1\n\n// after\nif len(arr) == 1:\n    val = arr.item()\nelse:\n    val = arr[0]  # or handle the multi-element case","handlingStrategy":"validation","validationCode":"if len(arr) != 1:\n    raise ValueError(f'expected length-1 array, got {len(arr)}')\nval = arr.item()","typeGuard":null,"tryCatchPattern":"try:\n    val = arr.item()\nexcept ValueError:\n    val = arr[0] if len(arr) else None","preventionTips":["Check len(arr) before calling .item().","Prefer arr.item(0) or arr[0] when you know the index, to avoid the length-1 requirement.","In query-style code, assert the filter yields a unique row before .item()."],"tags":["extension-array","numpy-compat","value-error","pandas"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}