API reference#

Exact, correctly rounded summation of floating-point columns for Polars.

The xsum() expression computes the exact sum of a Float32 or Float64 column and rounds it once to the column’s type; rolling_xsum() and rolling_xsum_by() do the same for every window of a fixed number of rows or of a key column. It uses Radford M. Neal’s small and large superaccumulators (arXiv:1505.05571) and is implemented in Rust.

polars_xsum.xsum(expr: IntoExprColumn) → Expr#

Return the exact sum of a float column, correctly rounded.

The values are summed without any rounding error, and only the final result is rounded (to nearest, ties to even). The result therefore does not depend on the order of the values, on how the column is chunked, or on the number of threads, and cancellation cannot destroy precision.

Parameters:
expr

A Float32 or Float64 expression, a column name, or a Series. Other dtypes, including integers and Float16, are rejected with a polars.exceptions.ComputeError. To opt in, cast explicitly (e.g. .cast(pl.Float64)). Keep in mind that integers beyond 2**53 are not exactly representable as Float64.

Returns:
pl.Expr

A scalar expression of the input’s dtype. It works in select, with_columns, group_by(...).agg(...) and over(...).

Notes

  • Nulls are skipped. An empty or all-null column sums to 0.0, as with polars.Expr.sum().

  • Any NaN gives NaN. Infinities of both signs give NaN. Otherwise an infinite value gives that infinity.

  • An exact sum beyond the dtype’s finite range gives ±inf. Intermediate overflow does not, e.g. [MAX, MAX, -MAX] sums to MAX.

  • An exact zero is returned as +0.0.

  • Float32 sums are rounded directly to Float32, with no double rounding through Float64.

  • Columns of at least 32Ki values are summed in parallel on the plugin’s own thread pool, whose size follows POLARS_MAX_THREADS (see thread_pool_size()). The result is the same for any number of threads.

  • In group_by(...).agg(...) and over(...), all groups are summed in a single plugin call, in parallel. Polars first gathers the values of each group, as it does for any non-built-in aggregation. With overlapping windows (rolling, group_by_dynamic), every window is gathered, so memory grows with the number of rows times the window length. For one sum per row, use rolling_xsum() or rolling_xsum_by() instead of rolling.

Examples

>>> import polars as pl
>>> from polars_xsum import xsum
>>> df = pl.DataFrame({"x": [1e100, 1.0, -1e100], "g": ["a", "a", "b"]})
>>> df.select(naive=pl.col("x").sum(), exact=xsum("x"))
shape: (1, 2)
┌───────┬───────┐
│ naive ┆ exact │
│ ---   ┆ ---   │
│ f64   ┆ f64   │
╞═══════╪═══════╡
│ 0.0   ┆ 1.0   │
└───────┴───────┘
>>> df.group_by("g", maintain_order=True).agg(xsum("x"))
shape: (2, 2)
┌─────┬─────────────┐
│ g   ┆ x           │
│ --- ┆ ---         │
│ str ┆ f64         │
╞═════╪═════════════╡
│ a   ┆ 1.0000e100  │
│ b   ┆ -1.0000e100 │
└─────┴─────────────┘
polars_xsum.rolling_xsum(expr: IntoExprColumn, window_size: int, *, min_samples: int | None = None, center: bool = False) → Expr#

Return the exact sum of every window of window_size rows.

This is the exact counterpart of polars.Expr.rolling_sum() (without weights). Each window sum is exact and rounded once, with the rules of xsum(), so it does not depend on the values outside the window.

Parameters:
expr

A Float32 or Float64 expression, a column name, or a Series.

window_size

Number of rows per window. The window of a row ends at that row, or is centred on it with center=True.

min_samples

Number of non-null values a window needs for a non-null result. Defaults to window_size.

center

Centre the window on its row instead of ending it there.

Returns:
pl.Expr

An expression of the input’s dtype and length.

Notes

  • Nulls are skipped. With min_samples=0, a window without values sums to 0.0.

  • The window slides over the column with a single exact accumulator: the value entering the window is added, the value leaving it subtracted. The cost per row is therefore independent of the window size. Long columns are processed in parallel on the plugin’s thread pool.

  • In over(...), the windows stay within each group. The plugin is then called once per group, so with many small groups the per-call overhead dominates.

Examples

>>> import polars as pl
>>> from polars_xsum import rolling_xsum
>>> df = pl.DataFrame({"x": [1e100, 1.0, -1e100]})
>>> df.with_columns(
...     naive=pl.col("x").rolling_sum(3, min_samples=1),
...     exact=rolling_xsum("x", 3, min_samples=1),
... )
shape: (3, 3)
┌─────────────┬────────────┬────────────┐
│ x           ┆ naive      ┆ exact      │
│ ---         ┆ ---        ┆ ---        │
│ f64         ┆ f64        ┆ f64        │
╞═════════════╪════════════╪════════════╡
│ 1.0000e100  ┆ 1.0000e100 ┆ 1.0000e100 │
│ 1.0         ┆ 1.0000e100 ┆ 1.0000e100 │
│ -1.0000e100 ┆ 0.0        ┆ 1.0        │
└─────────────┴────────────┴────────────┘
polars_xsum.rolling_xsum_by(expr: IntoExprColumn, by: IntoExprColumn, window_size: timedelta | str, *, min_samples: int = 0, closed: ClosedInterval = 'right') → Expr#

Return the exact sum of every window of a key column.

This is the exact counterpart of polars.Expr.rolling_sum_by(). The window of a row holds the rows whose key lies in (key - window_size, key] (by default), and its sum is exact and rounded once, with the rules of xsum().

Parameters:
expr

A Float32 or Float64 expression, a column name, or a Series.

by

The key column: an integer, Date or Datetime column. It need not be sorted.

window_size

The length of the windows: a number of key units such as "3i" for an integer key, otherwise a duration such as "2d", "1mo" or "1d12h" (the language of polars.Expr.dt.offset_by(), so calendar units and time zones are respected), or a timedelta.

min_samples

Number of non-null values a window needs for a non-null result. The default, 0, is that of Polars 2 (Polars 1 defaults to 1).

closed

Which ends of the window interval are included: "right" (default), "left", "both" or "none".

Returns:
pl.Expr

An expression of the input’s dtype and length.

Notes

  • Nulls are skipped. A row with a null key gives null and belongs to no window.

  • Rows are sorted by key if necessary. The window then slides over them with a single exact accumulator, adding values that enter it and subtracting values that leave it, so the cost per row does not depend on the window size.

  • In over(...), the windows stay within each group. The plugin is then called once per group, so with many small groups the per-call overhead dominates.

Examples

>>> from datetime import date
>>> import polars as pl
>>> from polars_xsum import rolling_xsum_by
>>> df = pl.DataFrame(
...     {
...         "day": [date(2024, 1, 1), date(2024, 1, 2), date(2024, 1, 4)],
...         "x": [1e100, 1.0, -1e100],
...     }
... )
>>> df.with_columns(
...     naive=pl.col("x").rolling_sum_by("day", "4d"),
...     exact=rolling_xsum_by("x", "day", "4d"),
... )
shape: (3, 4)
┌────────────┬─────────────┬────────────┬────────────┐
│ day        ┆ x           ┆ naive      ┆ exact      │
│ ---        ┆ ---         ┆ ---        ┆ ---        │
│ date       ┆ f64         ┆ f64        ┆ f64        │
╞════════════╪═════════════╪════════════╪════════════╡
│ 2024-01-01 ┆ 1.0000e100  ┆ 1.0000e100 ┆ 1.0000e100 │
│ 2024-01-02 ┆ 1.0         ┆ 1.0000e100 ┆ 1.0000e100 │
│ 2024-01-04 ┆ -1.0000e100 ┆ 0.0        ┆ 1.0        │
└────────────┴─────────────┴────────────┴────────────┘
polars_xsum.thread_pool_size() → int#

Returns the number of threads of the plugin’s thread pool.

xsum, rolling_xsum and rolling_xsum_by use this pool for long inputs.

The plugin has its own thread pool (it cannot share the Polars host’s). Its size follows POLARS_MAX_THREADS and defaults to the number of available CPUs; RAYON_NUM_THREADS overrides it. Both are read once, when the pool is first used.

polars_xsum.__version__: str#

The version of the installed package.

Type aliases#

These aliases appear in the signatures above. They exist for type checkers only and cannot be imported at runtime.

type polars_xsum.IntoExprColumn = Expr | str | Series#

An expression, the name of a column, or a polars.Series.

type polars_xsum.ClosedInterval = Literal['left', 'right', 'both', 'none']#

Which ends of a window interval are included.