Skip to main content

Polars plugin implementing argpartition function

Project description

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.

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_argpartition-0.0.3.tar.gz (45.5 kB view details)

Uploaded Source

Built Distributions

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

polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.8+Windows x86-64

polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl (3.7 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file polars_argpartition-0.0.3.tar.gz.

File metadata

  • Download URL: polars_argpartition-0.0.3.tar.gz
  • Upload date:
  • Size: 45.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.2

File hashes

Hashes for polars_argpartition-0.0.3.tar.gz
Algorithm Hash digest
SHA256 aa0cfff5822ed4d369e4404db4c80cc95d03ff92319245c2631c63ba609b6621
MD5 31778d2b80a6c3115eeb7c71f52fed5b
BLAKE2b-256 c2949da858298f1bda1aca6fdf9e4765289207385014290a94f8c2f2753bad2e

See more details on using hashes here.

File details

Details for the file polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_argpartition-0.0.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b4caeafc167ab6a071db3f7fe2b008eabad909f23277bd24269b919cc14f5095
MD5 83d5f3dfa4f9691d3dce686e74e3ed5f
BLAKE2b-256 75a1c1db2e456595fe1f3db1fbddfe11c2ee1eb7033e455eef54fcc1e0b3ca0a

See more details on using hashes here.

File details

Details for the file polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 09b916aed28727060b4654bf352fb33421333250bc39b8f6006f00129491364a
MD5 2c7c53e47c254f86dc61f82f2876aee9
BLAKE2b-256 2320928f04b3f549b1dc6c3892123f6b6b9f49db7bb5b7013f5f279b48155dd5

See more details on using hashes here.

File details

Details for the file polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_argpartition-0.0.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e25eb5a660f885a5c6ab68560895e0a4fd2688ff8b40b555af4946f3c7ce1992
MD5 d1cfa999505cf627252d1aa72592a279
BLAKE2b-256 a1e4b255de615d390020ead7e00b4e1928de3bcc60addce178b0a2c6005065ee

See more details on using hashes here.

File details

Details for the file polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_argpartition-0.0.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f055c987e6c9ddc2d6f718646a62f592c5bfcac5c0333942d324c23685cb8483
MD5 b0ab3beb9188c38376aaf878c8436b7e
BLAKE2b-256 a77ebe302bdb53a9c1407d0e2da127ecf729a130bc977af5334c17e6edf01c1d

See more details on using hashes here.

File details

Details for the file polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_argpartition-0.0.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6218579c937dc12c23c2562b5571daf1012accc7d6970e5f25d81e0fd08a2295
MD5 ce3afd403aa5a025adfa74ec999219c5
BLAKE2b-256 ced19b8be5f495986402ccb2c05709d540f856b38559b12e6fdaae91fd2330a9

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