User guide#

Semantics#

Input

Result

Float64 column

Float64, the exact sum correctly rounded

Float32 column

Float32, rounded directly from the exact sum (no double rounding through Float64)

any other dtype (integers, Float16, Decimal, …)

ComputeError; cast explicitly to opt in

nulls

skipped; an empty or all-null column gives 0.0 (like pl.sum)

any NaN

NaN (for Float64, the payload of the first NaN is kept)

both +inf and -inf

NaN

otherwise an infinity

that infinity

exact sum beyond the finite range

±inf (incl. the tie at MAX + ½ ulp, as in IEEE-754)

exact zero

+0.0

xsum is a scalar aggregation. It works in select, with_columns (broadcast), group_by(...).agg(...), over(...), and in lazy and streaming queries. In group_by and over, a single plugin call sums all groups. With overlapping windows (rolling, group_by_dynamic), Polars gathers every window first, so memory grows with the number of rows times the window length.

xsum also accepts a column name or a Series. To sum a Series directly, select the expression:

s = pl.Series([1e100, 1.0, -1e100])
pl.select(xsum(s)).item()  # 1.0

Rolling sums#

rolling_xsum(expr, window_size, *, min_samples=None, center=False) is the exact counterpart of pl.col(...).rolling_sum(...): every window of window_size rows is summed exactly and rounded once, with the rules above. A single accumulator slides over the column, adding the value that enters the window and subtracting the one that leaves it. Both steps are exact, so the cost per row does not depend on the window size: ~1.7× the time of the inexact rolling_sum, more for windows approaching the column length (see Benchmarks).

rolling_xsum_by(expr, by, window_size, *, min_samples=0, closed="right") is the exact counterpart of pl.col(...).rolling_sum_by(...): with the default closed="right", the window of a row holds the rows whose key lies in (key - window_size, key]. The key is an integer column (window_size="3i") or a Date or Datetime column, with a duration such as "2d", "1mo" or a timedelta; calendar units and time zones follow dt.offset_by. The key need not be sorted. This is about as fast as the inexact rolling_sum_by, except for windows approaching the column length. The default min_samples=0 follows Polars 2 (Polars 1 defaults to 1).

For one sum per row, prefer these two to xsum in rolling(...), which gathers every window first. xsum remains the choice for group_by_dynamic(...), which gives one row per window.

In over(...), the windows of both functions stay within each group. Unlike xsum, which sums all groups in one plugin call, they are called once per group, so with many small groups the per-call overhead dominates: with 10^5 groups of 4 rows, they take about 2.5× the time of their inexact counterparts.

Threading#

Columns of at least 32Ki values are summed in parallel. A plugin cannot use the thread pool of the Polars host process: it is a separate library with its own copies of Polars and rayon. xsum therefore has its own pool with threads named xsum-0, xsum-1, …, created on first use. It is sized exactly like Polars’ pool:

  • POLARS_MAX_THREADS sets the number of threads, parsed with Polars’ own configuration code;

  • without it, the pool uses the number of available CPUs;

  • RAYON_NUM_THREADS overrides the size for this plugin only.

polars_xsum.thread_pool_size() reports the size. While the pool works, the calling Polars thread sleeps, so concurrent xsum expressions share the pool instead of oversubscribing the CPU. The groups of a group_by or over are summed in parallel when they hold at least 32Ki values in total. Shorter inputs are summed on the calling Polars thread. The result is bit-identical for any number of threads.

Not supported#

  • Other dtypes: integers, Decimal and Float16 are rejected rather than converted silently. Cast them explicitly, keeping in mind that integers beyond 2^53 are not exactly representable as Float64.

  • Weighted rolling sums: rolling_xsum has no weights parameter.

  • Other exact reductions: there is no exact sum_horizontal, cum_sum or mean. A mean computed as xsum("x") / pl.col("x").count() is rounded twice (the sum, then the division), so it is within about one ulp of the exact mean, but not necessarily correctly rounded.

  • Method syntax: xsum is a function, not an expression namespace, so write xsum("x") or xsum(pl.col("x")) rather than pl.col("x").xsum().