{"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":1656,"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":1638,"sourceCodeEnd":1674,"githubUrl":"https://github.com/pola-rs/polars/blob/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/series/series.py#L1638-L1674","documentation":"Item-extraction guard: .item() without an explicit index requires the Series to have exactly one element; a longer Series triggers the error unless an index is passed. Fires on any multi-row Series with index=None.","triggerScenarios":"Thrown at py-polars/src/polars/series/series.py:1771 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Either reduce the Series to length 1 first (e.g. `s.filter(...)`), or pass an explicit index: `s.item(0)`."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}