Polars argpartition
A Polars plugin that implements the argpartition function in Rust.
Under the hood, it uses the select_nth_unstable method from the Rust standard library.
Installation
pip install polars-argpartition
Usage
import polars as pl
from polars_argpartition import argpartition
df = pl.DataFrame(
{
"a": [1, 3, 6, 2, 5, 10, 12],
}
)
print(df.with_columns(idxs=argpartition(pl.col("a"), k=3)))
Output (the order may change between runs, but the element at index 3 will always be the 4th smallest element):
shape: (7, 2)
┌─────┬───────┐
│ a ┆ idxs │
│ --- ┆ --- │
│ i64 ┆ u32 │
╞═════╪═══════╡
│ 1 ┆ 0 │
│ 3 ┆ 3 │
│ 6 ┆ 1 │
│ 2 ┆ 4 │
│ 5 ┆ 2 │
│ 10 ┆ 5 │
│ 12 ┆ 6 │
└─────┴───────┘
Another use case of this function is to get top-k elements in a column without caring about their order. You can do that as follows
df = pl.DataFrame(
{
"a": [1, 3, 6, 2, 5, 10, 12],
}
)
k = 3
print(
df.with_columns(
idxs=argpartition(
-pl.col("a"),
k=k,
)
)
.with_row_index(name="row_index")
.filter(pl.col("row_index").is_in(pl.col("idxs").slice(0, k)))
.select(["a"])
)
Output:
shape: (3, 1)
┌──────┐
│ a │
│ --- │
│ i64 │
╞══════╡
│ 6 │
│ 10 │
│ 12 │
└──────┘
Acknowledgments
A huge thank you to Marco Gorelli for his fantastic Polars plugin tutorial, which served as the foundation for this project.
Release files for polars-argpartition 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| polars_argpartition-0.0.3.tar.gz | 45.5 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl | CPython 3.8 | abi3 | Windows x86-64 | Details |
| polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl | CPython 3.8 | abi3 | macOS 11.0+ ARM64 | Details |
| polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl | CPython 3.8 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 20.2 MB
Release files / polars_argpartition-0.0.3.tar.gz
| Download URL | polars_argpartition-0.0.3.tar.gz |
|---|---|
| Size | 45.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aa0cfff5822ed4d369e4404db4c80cc95d03ff92319245c2631c63ba609b6621
|
|
BLAKE2b-256 checksum How to use checksums |
c2949da858298f1bda1aca6fdf9e4765289207385014290a94f8c2f2753bad2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|
Release files / polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl
| Download URL | polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl |
|---|---|
| Size | 3.8 MB |
| Tags | CPython 3.8 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
b4caeafc167ab6a071db3f7fe2b008eabad909f23277bd24269b919cc14f5095
|
|
BLAKE2b-256 checksum How to use checksums |
75a1c1db2e456595fe1f3db1fbddfe11c2ee1eb7033e455eef54fcc1e0b3ca0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|
Release files / polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 4.6 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
09b916aed28727060b4654bf352fb33421333250bc39b8f6006f00129491364a
|
|
BLAKE2b-256 checksum How to use checksums |
2320928f04b3f549b1dc6c3892123f6b6b9f49db7bb5b7013f5f279b48155dd5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|
Release files / polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 4.1 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
e25eb5a660f885a5c6ab68560895e0a4fd2688ff8b40b555af4946f3c7ce1992
|
|
BLAKE2b-256 checksum How to use checksums |
a1e4b255de615d390020ead7e00b4e1928de3bcc60addce178b0a2c6005065ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|
Release files / polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl
| Download URL | polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 3.7 MB |
| Tags | CPython 3.8 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f055c987e6c9ddc2d6f718646a62f592c5bfcac5c0333942d324c23685cb8483
|
|
BLAKE2b-256 checksum How to use checksums |
a77ebe302bdb53a9c1407d0e2da127ecf729a130bc977af5334c17e6edf01c1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|
Release files / polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl
| Download URL | polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 4.0 MB |
| Tags | CPython 3.8 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
6218579c937dc12c23c2562b5571daf1012accc7d6970e5f25d81e0fd08a2295
|
|
BLAKE2b-256 checksum How to use checksums |
ced19b8be5f495986402ccb2c05709d540f856b38559b12e6fdaae91fd2330a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.8.2
|