Skip to main content

polars-list-math

polars-list-math helps you work with Polars DataFrames in which every field of a nested list of structs is represented by its own column. Each field column preserves the original list nesting, so corresponding values remain aligned by row and by position at every list level.

For example, a nested list of records with price and qty fields can be represented as two nested list columns in the same DataFrame:

flowchart LR
    records["orders: List[List[Struct]]<br/>[[{price: 10, qty: 2}, {price: 20, qty: 1}],<br/>[{price: 30, qty: 4}]]"]
    subgraph columns["Separate field columns — same DataFrame row"]
        price["orders.price: List[List[Int64]]<br/>[[10, 20], [30]]"]
        qty["orders.qty: List[List[Int64]]<br/>[[2, 1], [4]]"]
    end
    records -->|price| price
    records -->|qty| qty

Each level of lists keeps its grouping, and each field becomes independently accessible as a column. The same representation extends to deeper nesting. The dotted column names above are illustrative.

The library provides Python/Rust helpers for calculations on these list columns, comparing lists, and combining corresponding fields with list_zip and ZipBuilder. It also includes JSON and URL expressions, null-field discovery, and schema cleaning for preparing and transforming DataFrames and LazyFrames.

Release notes and upgrade guidance are recorded in CHANGELOG.md.

Import the package once to register extra methods on Expr.list and Expr.str:

import polars as pl
import polars_list_math  # noqa: F401

For type-checked code, import the top-level expression helpers described below.

Install

pip install polars-list-math
# or
uv add polars-list-math

Requires Python 3.12+ and polars>=1.39.3.

Functions

Top-level functions provide the statically typed API for Pyright, Pylance, and other type checkers. Where available, the optional Method column lists an equivalent runtime method registered when the package is imported.

Function Method (optional) Result Docs
list_zip(...) Expr.list.zip(...) Zip lists into list[struct] List zip
nested_join(...) — Attach flat values using nested and ancestor keys Nested join
nested_lookup(...) — Look up a shared or row-local dictionary, preserving List structure Nested join
list_combinations(...) Expr.list.combinations(...) Pair items in one list Combinations
list_combinations_to(...) Expr.list.combinations_to(...) Pair items from two lists Combinations
expected_value_of_game(...) — Expected value of a sequential game Expected value of a game
list_similarity(...) Expr.list.similarity(...) Weighted similarity between two lists Similarity
list_mean_similarity(...) Expr.list.mean_similarity(...) Mean similarity inside nested lists Similarity
list_mean_similarity_to(...) Expr.list.mean_similarity_to(...) Mean similarity to reference nested lists Similarity
list_containment(...) Expr.list.containment(...) Fraction of unique IDs found in another list Containment
list_mean_containment(...) Expr.list.mean_containment(...) Mean directed containment against peer lists Containment
list_mean_containment_to(...) Expr.list.mean_containment_to(...) Mean directed containment against reference lists Containment
json_object_items(expr, strict=False) Expr.str.json_object_items(...) Convert a JSON object to list[struct[key, value]] JSON object items
json_array_values(expr, strict=False) Expr.str.json_array_values(...) Convert a JSON array to list[str] JSON array values
url_query_encode(expr, doseq=False) — Encode a Struct or key/value list as a URL query string URL query encoding
url_build(...) — Assemble a URL from optional component expressions URL building
dfs_to_lazy_df(...) — Expose DataFrame batches as a pushdown-aware LazyFrame source Examples
find_null_schema(lf) — Discover absent columns and nested fields Null schemas
clean_schema(schema, subschema) — Remove columns and fields described by a subschema Null schemas
apply_schema(lf, schema) — Select and cast a LazyFrame to the cleaned schema Null schemas

The Python helper py_list_similarity(...) computes weighted similarity for plain Python sequences. py_list_containment(...) computes directed set containment for Python sequences, ignoring ranks and duplicates.

The Expr.list and Expr.str methods are registered dynamically and remain available as convenient runtime syntax. Python type checkers cannot safely augment Polars' own namespace classes from another installed package, so use the equivalent top-level functions in code checked by Pylance or Pyright.

If a future Polars release ships native methods with the same names, this package leaves the native implementation untouched.

Quick Examples

from polars_list_math import (
    list_combinations,
    list_mean_similarity,
    list_similarity,
    list_zip,
)

df = pl.DataFrame(
    {
        "a": [[1, 2, 3]],
        "b": [[2, 1, 3]],
        "groups": [[[1, 2, 3], [1, 2, 3], [4, 5, 6]]],
    }
)

df.with_columns(
    list_similarity("a", "b").alias("similarity"),
    list_mean_similarity("groups").alias("mean_similarity"),
    list_combinations("a").alias("pairs"),
    list_zip("a", "b", fields=["a", "b"]).alias("zipped"),
)

Convert JSON strings to nested Polars values. String values stay strings, JSON null stays null, and other values become compact JSON strings. Invalid JSON returns null by default; pass strict=True to raise instead.

from polars_list_math import json_array_values, json_object_items

json_df = pl.DataFrame(
    {
        "object": ['{"name":"Ada","active":true,"note":null}'],
        "array": ['["python",42,true,null]'],
    }
)

json_df.select(
    json_object_items("object").alias("items"),
    json_array_values("array").alias("values"),
)

Encode query parameters and build URLs from expressions:

from polars_list_math import url_build, url_query_encode

pages = pl.DataFrame(
    {
        "host": ["example.com"],
        "path": ["/search"],
        "term": ["rust polars"],
        "page": [2],
    }
)

query = url_query_encode(pl.struct(q=pl.col("term"), page=pl.col("page")))

pages.select(
    url_build(
        scheme=pl.lit("https"),
        netloc="host",
        path="path",
        query=query,
    ).alias("url")
)

More complete, runnable examples are available in the examples directory.

Nested zip builder

ZipBuilder combines nested list columns with zip options and ordered eval/filter operations at each level. build() returns a normal Polars expression:

from polars_list_math import ZipBuilder

expr = (
    ZipBuilder("a", "b")
    .level(pad=True)
    .filter(pl.element().struct.field("a").is_not_null())
    .build("nested_zip")
)

The constructor configures the outer zip; each level() adds one inner zip. See the builder guide for transformations, broadcasting, and deeper nesting.

Performance experiments

Reproducible performance experiments live in the performance directory. Each experiment has its own README with the tested scenario, run instructions, environment, and recorded results.

The current dataframe_construction experiment compares construction of 30-column Polars DataFrames from a regular dataclass, a slotted dataclass, and a named tuple, including nested structs and lists.

uv run pytest performance/dataframe_construction -v -s

Development

make install
make develop
make test

Build and check the package locally:

make check-dist

Release steps are in docs/publishing.md.

Metadata

Release files for polars-list-math 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for polars-list-math 0.8.0
File Size Uploaded Provenance
polars_list_math-0.8.0.tar.gz 57.9 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for polars-list-math 0.8.0
File
polars_list_math-0.8.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
polars_list_math-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
polars_list_math-0.8.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
polars_list_math-0.8.0-cp313-cp313-macosx_10_12_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.12+ x86-64 Details
polars_list_math-0.8.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
polars_list_math-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
polars_list_math-0.8.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
polars_list_math-0.8.0-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details

Total release size: 47.6 MB

Release files / polars_list_math-0.8.0.tar.gz

Download URL polars_list_math-0.8.0.tar.gz
Size 57.9 kB
Tags Source
SHA-256 checksum
How to use checksums
62da70eae941c68819c8f73d04c2b4799e70f5066aa97d33aa96fc6b484e8482
BLAKE2b-256 checksum
How to use checksums
e55523d0e0ee4a3a0d76e7e6e217bb3af90040c8ce143498c99268c01c77f3ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp313-cp313-win_amd64.whl

Download URL polars_list_math-0.8.0-cp313-cp313-win_amd64.whl
Size 6.0 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
d6aa18a1314172d0b1e1950ad243e7abe24e64d9c295619ed24b3cd1ea693755
BLAKE2b-256 checksum
How to use checksums
4424d7598da81a04fddd115c768fe85a08c1e6673a2269a42a4e05fc0e3a5be2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_list_math-0.8.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.4 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7b58cfa63f544412f909f11213b11fffc8766f33d37ad3cbecb6f4945b3766d0
BLAKE2b-256 checksum
How to use checksums
9a5b2ad0d8df8d3fc0c86bbf4a5697e30bc11c26f2f0a96a6fae4f6794a813e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL polars_list_math-0.8.0-cp313-cp313-macosx_11_0_arm64.whl
Size 5.5 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f711b1ef56ed5466f8cbd37ea038ca143083a57992e8f810610bfb872fecab76
BLAKE2b-256 checksum
How to use checksums
66caca1f88cf5cca0a93b2ff1d1c8a64a8fc031ee5c0c37559f1e6b6bc7ede9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp313-cp313-macosx_10_12_x86_64.whl

Download URL polars_list_math-0.8.0-cp313-cp313-macosx_10_12_x86_64.whl
Size 5.9 MB
Tags CPython 3.13 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
bdac540ef9a5515d4b4090e8c3da509b0229b13c591c3fcb575fac4bfc68ef85
BLAKE2b-256 checksum
How to use checksums
84fea9f8197d6a115f3217019f522d5bd6241f94dd768ac2184370e2afd5109c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp312-cp312-win_amd64.whl

Download URL polars_list_math-0.8.0-cp312-cp312-win_amd64.whl
Size 6.0 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
dba41f54f0cfda4ff70c0b2ecee33aa6cf0c210bf1265d628cc9a3409d8dcd5b
BLAKE2b-256 checksum
How to use checksums
a518540cf9de734b15c756e7b3b908446d436cbfa99c8c4a388b9c1a9281259c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_list_math-0.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 6.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
fac743155a055768a547fe8e8468b4dac19827cf8e1323390641388fd50ff6ef
BLAKE2b-256 checksum
How to use checksums
57af4d4af7a322751c550ca60ff0f77b05d00e3d5ef36f25fa3322b6884fb458
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL polars_list_math-0.8.0-cp312-cp312-macosx_11_0_arm64.whl
Size 5.5 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7a81234aa9c5dcefcb18208d8733fa74cfe3fcdee5239d666c2cec11a67911b3
BLAKE2b-256 checksum
How to use checksums
9d85212649f6ba4c532f7ea5f682b32a24e93108e1009002402fc7fccc56d768
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / polars_list_math-0.8.0-cp312-cp312-macosx_10_12_x86_64.whl

Download URL polars_list_math-0.8.0-cp312-cp312-macosx_10_12_x86_64.whl
Size 5.9 MB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
2dc510487de764a79674216a6259059263e4913358cd589a45c7e796e25c28fe
BLAKE2b-256 checksum
How to use checksums
774f886c35e532d33d69a7445b7465f6900a6f604c8907828318cd7cf0997d25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Security for polars-list-math 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 9 files in this release have been uploaded with provenance metadata.

Select from 9 files

Attestation for polars_list_math-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page