User guide#
Semantics#
Input |
Result |
|---|---|
|
|
|
|
any other dtype (integers, |
|
nulls |
skipped; an empty or all-null column gives |
any NaN |
NaN (for |
both |
NaN |
otherwise an infinity |
that infinity |
exact sum beyond the finite range |
|
exact zero |
|
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_THREADSsets the number of threads, parsed with Polars’ own configuration code;without it, the pool uses the number of available CPUs;
RAYON_NUM_THREADSoverrides 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,
DecimalandFloat16are rejected rather than converted silently. Cast them explicitly, keeping in mind that integers beyond 2^53 are not exactly representable asFloat64.Weighted rolling sums:
rolling_xsumhas noweightsparameter.Other exact reductions: there is no exact
sum_horizontal,cum_sumormean. A mean computed asxsum("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:
xsumis a function, not an expression namespace, so writexsum("x")orxsum(pl.col("x"))rather thanpl.col("x").xsum().