polars-xsum#

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

polars-xsum is a Polars expression plugin that provides xsum, an exact replacement for pl.col(...).sum() on float columns. It computes the exact sum of all values and rounds only once at the end. It does this using an independent Rust implementation of Radford M. Neal’s small and large superaccumulators (Neal 2015, arXiv:1505.05571). Long columns are summed at memory bandwidth on all cores.

import polars as pl
from polars_xsum import xsum

df = pl.DataFrame({"x": [1e100, 1.0, -1e100]})
df.select(naive=pl.col("x").sum(), exact=xsum("x"))
# ┌───────┬───────┐
# │ naive ┆ exact │
# ╞═══════╪═══════╡
# │ 0.0   ┆ 1.0   │
# └───────┴───────┘

Why?#

Floating-point addition rounds after every step, so ordinary summation can lose any amount of precision. It also depends on the order of the values and on how the data is chunked or parallelised. xsum returns the exact sum rounded once to nearest (ties to even). The result is:

  • as accurate as possible: the correctly rounded exact sum;

  • deterministic: independent of value order, chunking and thread count;

  • robust: intermediate overflow is impossible, so [MAX, MAX, -MAX] sums to MAX.

Installation#

pip install polars-xsum    # or: uv add polars-xsum

Supported versions: Python ≥ 3.11 and Polars ≥ 1.28. CI runs the full test suite against the oldest supported Polars (1.28.0) and against the version pinned in uv.lock, which Dependabot keeps current. Wheels are available for Linux with glibc (manylinux; x86_64 and aarch64), Windows (x64) and macOS (x86_64 and arm64). On other platforms, such as Alpine Linux (musl), pip builds the package from source, which requires Rust ≥ 1.95.