Skip to main content

Native Polars expression plugin for list.zip returning list[struct].

Project description

polars-list-zip-struct

polars-list-zip-struct is a native expression plugin for the accepted Polars list.zip proposal from pola-rs/polars#22719.

It adds:

  • pl.col("a").list.zip(pl.col("b"), pad=False)
  • pl.zip_list("a", "b", "c", pad=False)

Both return a list[struct] expression. By default, lists are truncated to the shortest input, like Python's zip. With pad=True, shorter lists are padded with nulls to match the longest input. Struct fields default to the input column/expression names, matching the upstream Polars pull request.

If a future Polars release ships native list.zip or pl.zip_list, this package leaves the native implementation untouched.

Installation

pip install polars-list-zip-struct

Usage

Import the package once to register the Polars helpers:

import polars as pl
import polars_list_zip_struct  # noqa: F401

df = pl.DataFrame(
    {
        "a": [[1, 2], [3], None, [None]],
        "b": [[10, 20], [30, 35], [40], [35]],
    }
)

out = df.with_columns(
    pl.col("a").list.zip(pl.col("b")).alias("zipped")
)
shape: (4, 3)
┌───────────┬───────────┬──────────────────┐
│ a         ┆ b         ┆ zipped           │
│ ---       ┆ ---       ┆ ---              │
│ list[i64] ┆ list[i64] ┆ list[struct[2]]  │
╞═══════════╪═══════════╪══════════════════╡
│ [1, 2]    ┆ [10, 20]  ┆ [{1,10}, {2,20}] │
│ [3]       ┆ [30, 35]  ┆ [{3,30}]         │
│ null      ┆ [40]      ┆ null             │
│ [null]    ┆ [35]      ┆ [{null,35}]      │
└───────────┴───────────┴──────────────────┘

Zip more than two list expressions with pl.zip_list:

df.with_columns(
    pl.zip_list("a", "b", "c", fields=["a", "b", "c"]).alias("all_zipped")
)

Use pad=True for zip_longest behavior:

df.with_columns(
    pl.col("a").list.zip(pl.col("b"), pad=True).alias("zipped")
)

Notes

The core implementation is a Rust Polars expression plugin built with pyo3-polars. It follows the original upstream implementation idea: build a LargeListArray of StructArray values with Arrow builders, offsets, and row validity, rather than running a Python row loop.

Development

python -m pip install -e ".[dev]"
maturin develop
python -m pytest

Build and upload:

python -m pip install ".[publish]"
maturin build --release
python -m twine upload target/wheels/*

Project details


Download files

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

Source Distribution

polars_list_zip_struct-0.1.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

polars_list_zip_struct-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file polars_list_zip_struct-0.1.0.tar.gz.

File metadata

  • Download URL: polars_list_zip_struct-0.1.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for polars_list_zip_struct-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b7f70ac9c81bf213e4ae2556c828880f7e4fe1e48386b52a0394ada3a76232db
MD5 91b892d861bdc9dc58eda209a5e519d3
BLAKE2b-256 d2bd3557e71189ce4d17618fbb61dcc465b81118007d73d7ffab8f4f2b6eb006

See more details on using hashes here.

File details

Details for the file polars_list_zip_struct-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for polars_list_zip_struct-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 0dc94b7ac9079535c320075cc6699fc4ff80f5466d15d15eb6dc3408dddfec0f
MD5 62e41832b32cf443ae8875eab264813c
BLAKE2b-256 a89763d1ab38cc828e8df1714596d62f22a8e5057f2d7b7b01f2ac63a0984f04

See more details on using hashes here.

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