pola-rs/polars · error · ValueError
can only call '.item()' if the Series is of length 1, or an…
Error message
can only call '.item()' if the Series is of length 1, or an explicit index is provided (Series is of length {len(self)}) What it means
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.
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
Example fix
// before value = srs.item() // after value = srs.item(0) if len(srs) else None
Defensive patterns
Strategy: validation
Validate before calling
value = srs.item(0) if len(srs) >= 1 else None # or check == 1 for no-index item()
Type guard
def single_value(srs: pl.Series) -> object | None:
return srs.item() if len(srs) == 1 else None Try / catch
try:
value = srs.item()
except ValueError:
value = srs.item(0) if len(srs) else None Prevention
- 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
When it happens
Trigger: 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.
Common situations: Assuming a filtered query returns exactly one row; extracting 'the' value from an aggregation that unexpectedly produced multiple groups; empty result sets.
Related errors
- can't convert to Decimal
- cannot initialize Series from DataFrame without any columns
- cannot select elements using key of type
- cannot select elements using Sequence with elements of type
- data type not supported by the interchange protocol
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/b52357e632bd4a7b.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/series/series.py:1657
If no index is provided, this is equivalent to `s[0]`, with a check
that the shape is (1,). With an index, this is equivalent to `s[index]`.
Examples
--------
>>> s1 = pl.Series("a", [1])
>>> s1.item()
1
>>> s2 = pl.Series("a", [9, 8, 7])
>>> s2.cum_sum().item(-1)
24
"""
if index is None:
if len(self) != 1:
msg = (
"can only call '.item()' if the Series is of length 1,"
f" or an explicit index is provided (Series is of length {len(self)})"
)
raise ValueError(msg)
return self._s.get_index(0)
return self._s.get_index_signed(index)
def estimated_size(self, unit: SizeUnit = "b") -> int | float:
"""
Return an estimation of the total (heap) allocated size of the Series.
Estimated size is given in the specified unit (bytes by default).
This estimation is the sum of the size of its buffers, validity, including
nested arrays. Multiple arrays may share buffers and bitmaps. Therefore, the
size of 2 arrays is not the sum of the sizes computed from this function. In
particular, [`StructArray`]'s size is an upper bound.
When an array is sliced, its allocated size remains constant because the buffer
unchanged. However, this function will yield a smaller number. This is because
this function returns the visible size of the buffer, not its total capacity.View on GitHub (pinned to fe841f959e)