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
The package supports Polars releases from roughly the last three months onward
and does not cap future Polars versions. For this release that means
polars>=1.39.3.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_list_zip_struct-0.1.1.tar.gz.
File metadata
- Download URL: polars_list_zip_struct-0.1.1.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53c63fb2e017314e3ddb368a6c0a84ab4a90b30f889bc320fe09b5c9179e8f51
|
|
| MD5 |
76e91d7dabe56b06f099f487b03be866
|
|
| BLAKE2b-256 |
8ae461418ae008f9eedfe0850e2a2bd07013d2d10df4a82f5f42e23bf195fafe
|
File details
Details for the file polars_list_zip_struct-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: polars_list_zip_struct-0.1.1-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 5.3 MB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25c440e5f080ba50ad8f97424c1c088914f36d3a74b16001592698378bb34155
|
|
| MD5 |
a633792e3d8258bf645a7d6cf2592330
|
|
| BLAKE2b-256 |
b37aa8814252218545e3134e26086e499a2ed1e92150121dce9673bd286baa6a
|