polars.Expr.sum#

Expr.sum() Expr[source]#

Get sum value.

Notes

  • Dtypes in {Int8, UInt8, Int16, UInt16} are cast to Int64 before summing to prevent overflow issues.

  • If there are no non-null values, then the output is 0. If you would prefer empty sums to return None, you can use pl.when(expr.count()>0).then(expr.sum()) instead of expr.sum().

Examples

>>> df = pl.DataFrame({"a": [-1, 0, 1]})
>>> df.select(pl.col("a").sum())
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│  0  │
└─────┘