Skip to main content

Polars List Utils (polist)

polist is a Python package that provides a set of utilities for working with List-type columns in Polars DataFrames, especially for signal processing and feature extraction.

So far these utilities comprise those that I found to be missing or lacking from the List namespace within the Polars library while I was working on a project at work that required extensive handling of signal data which I was storing in Polars DataFrames.

By providing these utilities as a Polars plugin, and thus not having to leave the Polars DataFrame for these operations, I was able to significantly speed up the processing of my data by benefiting from Polars query optimization and parallelization. So while the operations themselves are not necessarily faster than their Numpy counterparts (although they might be in some cases), the integration with Polars gave my larger processing pipeline a significant speed boost.

Features

  • polist.apply_interp

    • Interpolates a new List-type column from 3 specified List-type columns.
    • Behaviour as expected from the numpy.interp function, but for Polars DataFrames.
    • Supply the x_column, y_column, and xp_column columns to obtain the interpolated y-values.
  • polist.apply_butterworth

    • Applies a Butterworth filter (low-pass, high-pass, band-pass) to a List-type column of signal data.
    • The filter is applied bidirectionally (like scipy.signal.filtfilt), so there is no phase lag, but the magnitude response is squared: the cutoff sits at -6dB instead of -3dB.
    • A band-pass doubles the design order, as in scipy.signal.butter.
  • polist.apply_fft

    • Applies a (real) Fast Fourier Transform (FFT) to a List-type column of signal data, producing the N/2 + 1 amplitudes from DC to Nyquist.
    • Can pre-process the signals with a windowing function (e.g. Hann, Blackman).
    • Can scale the amplitudes following the scipy conventions: amplitude reads a tone's peak amplitude, power its mean-square (scipy.signal.periodogram(scaling="spectrum")), and psd its power density (scaling="density", integrates to the signal's mean-square power).
    • The length of each signal must be a power of two, and the corresponding frequency axis is given by [i * sample_rate / N for i in range(N // 2 + 1)].
  • polist.agg_slices

    • Computes an aggregation of a range of y-values defined by some x-values for List-type columns.
    • This is useful for feature extraction from signals, e.g. to compute the mean of a signal in a certain time range or a spectrum in a certain frequency range.
    • Ranges to include or exclude can be given as simple (lo, hi) tuples or with explicit boundary modes like ((lo, "closed"), (hi, "open")).
  • polist.agg_lists

    • Applies element-wise list-aggregations to a List-type column in a GroupBy context.
    • This is implemented in pure Polars (no plugin), and lives here so that both aggregation functions offer the same aggregations with the same missing-data behaviour.

The four plugin functions accept a length-1 literal list column (e.g. pl.lit([...])) for any input and broadcast it across rows. Invalid configuration (bad cutoffs, unknown window names, mismatched list lengths) raises an error instead of silently returning nulls, while per-row data problems (e.g. a signal length that is not a power of two) yield null rows.

Both aggregation functions support mean, median, std, min, max, delta, and count, and both handle missing data exactly like the vertical aggregations of Polars itself:

  • Nulls are skipped: excluded from both the numerator and the denominator, so count only counts non-null values and an empty or all-null selection yields null (count: 0).
  • NaNs propagate: Polars treats NaN as a legitimate float value, so it flows into the sum and poisons mean and std. Two exceptions to be aware of are min and max, which skip NaNs (unless all values are NaN), and median sorts NaN as the largest value.
  • std is the sample standard deviation (ddof=1) and yields null for fewer than two values, as in Polars.

The three transforms instead turn any row with missing data (a null row, an empty list, or a list containing nulls) into a null output row: a signal with missing samples cannot be meaningfully transformed.

Example

import polars as pl
import polars_list_utils as polist

Fs, N = 200.0, 1024
FREQS = [i * Fs / N for i in range(N // 2 + 1)]

df = (
    df
    .with_columns(
        polist.apply_butterworth(
            "signal",
            sample_rate=Fs,
            max_freq=50.0,
        )
        .alias("filtered")
    )
    .with_columns(
        polist.apply_fft(
            "filtered",
            sample_rate=Fs,
            window="hann",
            scaling="amplitude",
        )
        .alias("fft")
    )
    .with_columns(
        polist.agg_slices(
            "fft",
            pl.lit(FREQS),
            aggregation="mean",
            slices_include=[(20.0, 30.0)],
        )
        .alias("mean_20_30hz")
    )
)

See examples/showcase.py for the full pipeline:

Showcase

Installation (user)

uv pip install polars-list-utils

Installation (developer)

  1. Setup Rust (i.e. install rustup)
  2. Setup Python (i.e. install uv)
  3. Setup environment and compile plugin:
uv sync --extra dev
uv run maturin develop --release --uv
  1. (Maybe) configure Cargo to find uv's Python installs. For example:
# .cargo/config.toml
[env]
PYO3_PYTHON = "C:\\Users\\travis.hammond\\AppData\\Roaming\\uv\\python\\cpython-3.12.0-windows-x86_64-none\\python.exe"
  1. Test and run the example:
uv run pytest
uv run ./examples/showcase.py
  1. Lint:
uvx ty check
uvx ruff check
cargo clippy --release -- -D warnings
cargo fmt

Metadata

Release files for polars-list-utils 1.0.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-utils 1.0.0
File Size Uploaded
polars_list_utils-1.0.0.tar.gz 315.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for polars-list-utils 1.0.0
File
polars_list_utils-1.0.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
polars_list_utils-1.0.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
polars_list_utils-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 23.6 MB

Release files / polars_list_utils-1.0.0.tar.gz

Download URL polars_list_utils-1.0.0.tar.gz
Size 315.8 kB
Tags Source
SHA-256 checksum
How to use checksums
db627646639bb861699c96f46bc03a1a7f3b1614b70b8c8fa0f5c7eb55be4bb9
BLAKE2b-256 checksum
How to use checksums
37c873ab23e694ad66f1a2b310ff5a214afe1ecb9d8f54c5d6b402988a83d85a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release files / polars_list_utils-1.0.0-cp310-abi3-win_amd64.whl

Download URL polars_list_utils-1.0.0-cp310-abi3-win_amd64.whl
Size 4.5 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
ca11885171bc221feaa28885be73e29a4d36c7541aff24f1d6ddf642bb22d3da
BLAKE2b-256 checksum
How to use checksums
55252e57fc8dd7fe38fca5396d8d89a1a31c710f45a2c9a70ebe9449ce63ea02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release files / polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 5.2 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
e508d7cac329568a537191803ce2d56d0b7ced53198131e2d59db2d28ff6e263
BLAKE2b-256 checksum
How to use checksums
bc9423d5465052b5ca317b1883918640272d9fa6b7ee58b1c144dea9b6a3b1d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release files / polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL polars_list_utils-1.0.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 5.0 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
bb85d68ddb99d90777825cfe91a3b91572686563f700e76e81348427de6cd706
BLAKE2b-256 checksum
How to use checksums
cc245b041d92b4f404a77e18ff8eea0b57537303c7e26dbc6780a857362e2c79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release files / polars_list_utils-1.0.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL polars_list_utils-1.0.0-cp310-abi3-macosx_11_0_arm64.whl
Size 4.2 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4945ed3477809c829ead73089ddeb991e3203cf9442eca2eb4c0d6bcc423129a
BLAKE2b-256 checksum
How to use checksums
b5c3048ef3946ddb9b8f0d9bddf8b8b51a4f19cdd5735eddadb394e54878592d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release files / polars_list_utils-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL polars_list_utils-1.0.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 4.4 MB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
18c0d53eb482bb1b5103cb05a75ba13c2d93b131ca5e3c974b66969341b4dc32
BLAKE2b-256 checksum
How to use checksums
e570df862b5b8b75fa138ea75314a4ac523fe7c81e770d4534f6cd8eee79e0a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.14.1

Release history Release notifications | RSS feed

2.0.0

6 release files

This release

1.0.0 This release

6 release files

0.9.1

13 release files

0.9.0

13 release files

0.1.0

14 release files

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