polars.Expr.item#

Expr.item(*, allow_empty: bool = False) Expr[source]#

Get the single value.

This raises an error if there is not exactly one value.

Parameters:
allow_empty

Allow having no values to return null.

See also

Expr.get()

Get a single value by index.

Examples

>>> df = pl.DataFrame({"a": [1]})
>>> df.select(pl.col("a").item())
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
└─────┘
>>> df = pl.DataFrame({"a": [1, 2, 3]})
>>> df.select(pl.col("a").item())
Traceback (most recent call last):
...
polars.exceptions.ComputeError: aggregation 'item' expected a single value, got 3 values
>>> df.head(0).select(pl.col("a").item(allow_empty=True))
shape: (1, 1)
┌──────┐
│ a    │
│ ---  │
│ i64  │
╞══════╡
│ null │
└──────┘