{"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":"validation","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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/base.py#L659-L695","documentation":"Raised by ExtensionArray.item(index=None) when index is None and the array length is not exactly 1. This mirrors numpy.ndarray.item(): with no index, only a single-element array can be converted to a scalar. Multi-element (or empty) arrays raise ValueError. Reached through pd.array(...).item() or Series.array.item().","triggerScenarios":"Calling `arr.item()` on an extension array with 0 or 2+ elements. Common after reductions/groupby that should return one value but unexpectedly return many (or none).","commonSituations":"Aggregations expected to yield a scalar; extracting a single config value from a filtered Series; asserting uniqueness of a query result.","solutions":["Pass an explicit index: `arr.item(0)` to get the first element regardless of length.","Ensure the array has exactly one element before calling item(): filter/slice first, e.g. `s[s>0].array.item()` only when unique.","Use `arr[0]` / `s.iloc[0]` if you just want the first value without the size-1 constraint.","Check `len(arr) == 1` before calling item() to give a clearer error to callers."],"exampleFix":"# before\narr = pd.array([1, 2, 3], dtype=\"Int64\")\narr.item()  # ValueError: can only convert an array of size 1\n\n# after\narr.item(0)   # explicit index\n# or\nsingle = pd.array([7], dtype=\"Int64\")\nsingle.item()","handlingStrategy":"validation","validationCode":"def safe_item(arr, index=None):\n    if index is None and len(arr) != 1:\n        raise ValueError(f\"len(arr)={len(arr)}; pass an explicit index or ensure exactly one element\")\n    return arr.item(index)","typeGuard":"def is_single_element(arr) -> bool:\n    return len(arr) == 1","tryCatchPattern":"try:\n    return arr.item()\nexcept ValueError as e:\n    if \"size 1\" in str(e):\n        return arr.item(0)\n    raise","preventionTips":["Pass an explicit index when you want a specific element regardless of length.","Check len(arr)==1 before scalar conversion in reductions.","Use arr[0]/s.iloc[0] when uniqueness is not guaranteed."],"tags":["extension-array","scalar-conversion","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}