Skip to main content

Native Polars expressions for piecewise-uniform distribution statistics

Project description

polars-distr

Native Polars expressions for statistics over piecewise-uniform distributions. The plugin computes means, CDF values, quantiles, and exact CRPS inside lazy and streaming query plans without Python UDFs.

Install with uv add polars-distr or pip install polars-distr.

import polars as pl
import polars_distr as pld  # registers the Distribution dtype

result = (
    pl.scan_parquet("distributions.parquet")
    .select(
        pld.quantiles("distribution", [0.1, 0.5, 0.9]).alias("quantiles"),
        pld.crps("distribution", "observation").alias("crps"),
    )
    .collect(engine="streaming")
)

The .distr accessor

Importing the package also registers a .distr expression namespace that delegates to these same functions:

pl.col("weights").distr.quantile(0.5, edges="edges")
pl.struct(weights="weights", edges="edges").distr.crps("observation")

It adds no semantics. It exists for callers that can only reach registered Polars namespaces and cannot import this package — notably YAML/JSON pipeline configs whose expression strings are evaluated with pl alone in scope. The subject is a Distribution column or raw Struct{weights, edges} storage; passing edges instead treats the subject as the weights, so a config never has to spell out pl.struct(...).

Named quantiles

Pass quantiles() a sequence of levels for a fixed-size Array in request order, or a name-to-level mapping for a Struct in mapping order — the same single traversal either way:

result = frame.select(
    pld.quantiles("distribution", {"p10": 0.1, "p50": 0.5, "p90": 0.9}).alias("q")
).unnest("q")  # -> p10, p50, p90 columns

Values and ordering are identical target for target. Validity differs in shape only: where the sequence form nulls the outer Array row, the mapping form yields a non-null Struct whose every field is null, so the unnested columns keep the row and carry the null per target.

Constructing distributions

distribution() accepts either paired fixed-width Array columns or paired variable-width List columns:

distribution = pld.distribution("weights", "edges")

It produces the logical Distribution dtype with serialized identity polars_distr.piecewise_uniform.v1. Its storage is a Struct with exactly weights and edges, in that order. Fixed storage must be Array/Array with edge width equal to weight width plus one. Ragged storage must be List/List. Mixed layouts are rejected by the constructor and statistics kernels.

Integer, Float32, and Float64 child values are supported. Invalid distribution rows produce null results; statically invalid schemas raise during schema resolution. Raw paired Struct storage is also accepted by every statistics kernel for interoperability.

v0.2 breaking changes

  • The public vocabulary is distribution, weights, and edges; the v0.1 histogram and counts names are removed.
  • The extension identity is polars_distr.piecewise_uniform.v1. Data written with the pre-contract polars_distr.distribution.v1 identity is not guaranteed to load.
  • distribution() accepts paired Array/Array and paired List/List storage. Mixed Array/List storage is rejected.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_distr-0.2.2-cp311-abi3-manylinux_2_28_x86_64.whl (7.0 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ x86-64

polars_distr-0.2.2-cp311-abi3-manylinux_2_28_aarch64.whl (6.4 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.28+ ARM64

polars_distr-0.2.2-cp311-abi3-macosx_11_0_arm64.whl (5.9 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file polars_distr-0.2.2-cp311-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polars_distr-0.2.2-cp311-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f1e5627a7cc6807f9dbdac7afaae2abc5742a76a251c2fdb5dbe238cbb4fa42
MD5 199ecf4f4b8b1726f8b339f9178a51b8
BLAKE2b-256 20841437118c71c9dda807f06a85f187e0f2c84605e5a450bf6ca8d9f93c223f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_distr-0.2.2-cp311-abi3-manylinux_2_28_x86_64.whl:

Publisher: release.yml on silurian-ai/polars-distr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_distr-0.2.2-cp311-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polars_distr-0.2.2-cp311-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fb686bbd6b4147c7981568192e65d68e8026b5a29a3ab471c2a6bc3a217e9516
MD5 d06e35ba70f3489b13685c5ffd7f6977
BLAKE2b-256 5a74b781a6ebf6ea1a8ca6b72778c64e53ada61d616f63051943973c44db8c80

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_distr-0.2.2-cp311-abi3-manylinux_2_28_aarch64.whl:

Publisher: release.yml on silurian-ai/polars-distr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_distr-0.2.2-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_distr-0.2.2-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4109ea801659ac23e4eecd17a67a51cca36e165b0ace5f90c9124a4ea9deae45
MD5 0e3465a14eedd4b1e079fb78b4df934b
BLAKE2b-256 f13f8fc8589d4ecdf18d03da66443f5f610b437074475b261493e74d8574d40f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_distr-0.2.2-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on silurian-ai/polars-distr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page