{"record":{"id":"e48b10afdf023475","repo":"pandas-dev/pandas","slug":"can-only-convert-an-array-of-size-1-to-a-python-sc-e48b10","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/base.py","lineNumber":438,"sourceCode":"        --------\n        Index.values : Returns an array representing the data in the Index.\n        Series.head : Returns the first `n` rows.\n\n        Examples\n        --------\n        >>> s = pd.Series([1])\n        >>> s.item()\n        1\n\n        For an index:\n\n        >>> s = pd.Series([1], index=[\"a\"])\n        >>> s.index.item()\n        'a'\n        \"\"\"\n        if len(self) == 1:\n            return next(iter(self))\n        raise ValueError(\"can only convert an array of size 1 to a Python scalar\")\n\n    @property\n    def nbytes(self) -> int:\n        \"\"\"\n        Return the number of bytes in the underlying data.\n\n        Includes only the memory used by the array values; overhead such as\n        the index is not included. Useful for estimating memory usage.\n\n        See Also\n        --------\n        Series.ndim : Number of dimensions of the underlying data.\n        Series.size : Return the number of elements in the underlying data.\n\n        Examples\n        --------\n        For Series:\n","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/base.py#L420-L456","documentation":"Raised by PandasObject.item() when the object's length is not exactly 1. Mirrors numpy's item() contract: only single-element containers can be reduced to a Python scalar. Calling on empty or multi-element Series/Index raises ValueError.","triggerScenarios":"`df['x'].item()` where len != 1; `s.index.item()` on multi-row data; common after aggregations that unexpectedly return >1 row.","commonSituations":"Asserting 'exactly one match' in queries; using .item() to unpack instead of .iloc[0]; empty results from filters.","solutions":["Check len(s) == 1 before calling .item().","Use .iloc[0] if you want the first element regardless of count.","Handle empty/multi cases explicitly with conditional logic."],"exampleFix":"// before\nv = df.loc[df.id == q, 'name'].item()\n\n// after\nsub = df.loc[df.id == q, 'name']\nv = sub.item() if len(sub) == 1 else sub.iloc[0]","handlingStrategy":"validation","validationCode":"if len(s) != 1:\n    raise ValueError(f'expected size 1, got {len(s)}')","typeGuard":"def is_single_element(s) -> bool:\n    return len(s) == 1","tryCatchPattern":"try:\n    v = s.item()\nexcept ValueError as e:\n    if 'size 1' in str(e):\n        v = s.iloc[0] if len(s) else None\n    else:\n        raise","preventionTips":["Check len before .item().","Use .iloc[0] when first-match semantics are acceptable.","Handle empty results explicitly."],"tags":["scalar","valueerror","item","size"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}