{"record":{"id":"b52357e632bd4a7b","repo":"pola-rs/polars","slug":"can-only-call-item-if-the-series-is-of-length","errorCode":null,"errorMessage":"can only call '.item()' if the Series is of length 1, or an explicit index is provided (Series is of length {len(self)})","messagePattern":"can only call '\\.item\\(\\)' if the Series is of length 1, or an explicit index is provided \\(Series is of length (.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1657,"sourceCode":"        If no index is provided, this is equivalent to `s[0]`, with a check\n        that the shape is (1,). With an index, this is equivalent to `s[index]`.\n\n        Examples\n        --------\n        >>> s1 = pl.Series(\"a\", [1])\n        >>> s1.item()\n        1\n        >>> s2 = pl.Series(\"a\", [9, 8, 7])\n        >>> s2.cum_sum().item(-1)\n        24\n        \"\"\"\n        if index is None:\n            if len(self) != 1:\n                msg = (\n                    \"can only call '.item()' if the Series is of length 1,\"\n                    f\" or an explicit index is provided (Series is of length {len(self)})\"\n                )\n                raise ValueError(msg)\n            return self._s.get_index(0)\n\n        return self._s.get_index_signed(index)\n\n    def estimated_size(self, unit: SizeUnit = \"b\") -> int | float:\n        \"\"\"\n        Return an estimation of the total (heap) allocated size of the Series.\n\n        Estimated size is given in the specified unit (bytes by default).\n\n        This estimation is the sum of the size of its buffers, validity, including\n        nested arrays. Multiple arrays may share buffers and bitmaps. Therefore, the\n        size of 2 arrays is not the sum of the sizes computed from this function. In\n        particular, [`StructArray`]'s size is an upper bound.\n\n        When an array is sliced, its allocated size remains constant because the buffer\n        unchanged. However, this function will yield a smaller number. This is because\n        this function returns the visible size of the buffer, not its total capacity.","sourceCodeStart":1639,"sourceCodeEnd":1675,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L1639-L1675","documentation":"Series.item() extracts a single scalar value; it only works when the Series has exactly one element, unless an explicit index is passed. Calling it on a longer (or empty) Series without an index raises ValueError with the Series length.","triggerScenarios":"srs.item() on a Series of length != 1 and index=None; also hit when a filter/select that was expected to yield one row actually yields zero or many.","commonSituations":"Assuming a filtered query returns exactly one row; extracting 'the' value from an aggregation that unexpectedly produced multiple groups; empty result sets.","solutions":["Pass an explicit index: srs.item(0)","Verify length first: if len(srs) == 1: srs.item()","Inspect why the Series has more/less than one element before extracting"],"exampleFix":"// before\nvalue = srs.item()\n// after\nvalue = srs.item(0) if len(srs) else None","handlingStrategy":"validation","validationCode":"value = srs.item(0) if len(srs) >= 1 else None  # or check == 1 for no-index item()","typeGuard":"def single_value(srs: pl.Series) -> object | None:\n    return srs.item() if len(srs) == 1 else None","tryCatchPattern":"try:\n    value = srs.item()\nexcept ValueError:\n    value = srs.item(0) if len(srs) else None","preventionTips":["Always verify len(srs) == 1 after filters before .item()","Pass an explicit index when length is known but > 1 semantics are intended","Log/assert expected row counts from filtered queries"],"tags":["polars","valueerror","scalar","series"],"backgroundTag":"index-out-of-range","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}