Polars plugin with pairing functions for nonnegative integers
Project description
polars-pairing
This plugin provides pairing functions to Polars. These functions encode two natural numbers into a single natural number.
Installation
pip install polars-pairing
Functionality and usage
This plugin implements three pairing functions (Hagen[^1], Szudzik[^2], and Cantor[^3]), along with their corresponding unpairing functions, as Polars expressions. The pairing functions take two integer columns as input and produce a single column containing the encoded pair. Conversely, the unpairing functions accept a single column as input and generate a struct column containing the values of the original pair.
>>> import polars as pl
>>> import polars_pairing as plp
>>> df = pl.DataFrame({
... "a": [1, 2, 38, 4],
... "b": [5, 6, 77, 80]
... })
>>> df.select(plp.col("b").pairing.pair(plp.col("b")).alias("pair"))
shape: (4, 1)
┌──────┐
│ pair │
│ --- │
│ u64 │
╞══════╡
│ 35 │
│ 48 │
│ 6083 │
│ 6560 │
└──────┘
# By default, the Hagen pairing function is used, but you can specify alternative functions:
>>> df.select(
... plp.col("a").pairing.pair(plp.col("b"), method="hagen").alias("hagen_pair"),
... plp.col("a").pairing.pair(plp.col("b"), method="szudzik").alias("szudzik_pair"),
... plp.col("a").pairing.pair(plp.col("b"), method="cantor").alias("cantor_pair")
)
shape: (4, 3)
┌────────────┬──────────────┬─────────────┐
│ hagen_pair ┆ szudzik_pair ┆ cantor_pair │
│ --- ┆ --- ┆ --- │
│ u64 ┆ u64 ┆ u64 │
╞════════════╪══════════════╪═════════════╡
│ 28 ┆ 26 ┆ 26 │
│ 40 ┆ 38 ┆ 42 │
│ 6006 ┆ 5967 ┆ 6747 │
│ 6408 ┆ 6404 ┆ 3650 │
└────────────┴──────────────┴─────────────┘
# The corresponding unpairing functions are also available:
>>> df = pl.DataFrame({"p": [28, 40, 6006, 6408]})
>>> df.select(plp.col("p").pairing.unpair().alias("unpair"))
shape: (4, 1)
┌───────────┐
│ unpair │
│ --- │
│ struct[2] │
╞═══════════╡
│ {1,5} │
│ {2,6} │
│ {38,77} │
│ {4,80} │
└───────────┘
>>> df = pl.DataFrame({"p": [26, 38, 5967, 6404]})
>>> df.select(
... plp.col("p").pairing.unpair(method="szudzik").alias("unpair")
... ).unnest("unpair")
shape: (4, 2)
┌──────┬───────┐
│ Left ┆ Right │
│ --- ┆ --- │
│ u64 ┆ u64 │
╞══════╪═══════╡
│ 1 ┆ 5 │
│ 2 ┆ 6 │
│ 38 ┆ 77 │
│ 4 ┆ 80 │
└──────┴───────┘
[^1]: Hagen, D. R. Superior Pairing Function. https://drhagen.com/blog/superior-pairing-function/ (2018). [^2]: Szudzik, Matthew. "An elegant pairing function." Wolfram Research (ed.) Special NKS 2006 Wolfram Science Conference. 2006. [^3]: Cantor, G. Ein Beitrag zur Mannigfaltigkeitslehre. Journal für die reine und angewandte Mathematik 1878, 242–258 (1878).
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 Distributions
Hashes for polars_pairing-0.1.0-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69e0d6b042adac5af8d13b77d6d4481838ad3e0d4c080e1473d460d1d522e293 |
|
MD5 | 5d34a9605e3cf9a967c0736f413c9e3a |
|
BLAKE2b-256 | beafa74af87a1325a62ce8eee0e87f413bf8d329ed4b6be133b435e823ce0c2a |
Hashes for polars_pairing-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0994b1e20148337283b44e93104b55d5c05a99a9cabda2f45b574b6f174f7189 |
|
MD5 | 6ac34c57cd6b828f1b6d65a827b9e3c8 |
|
BLAKE2b-256 | d3a7dac2b3a021dd1da3778d9369cf796fea99d75cac15e5da46104b228d764d |
Hashes for polars_pairing-0.1.0-cp38-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bce4c77d6788981207ef07888c5eacc6d9448ec353ad6c19f8700657b30cfac |
|
MD5 | 31a1311ddce250334948341588192104 |
|
BLAKE2b-256 | 59aac9df22c93604dcfb1d704d1223acade3e7527d1240f01fc49eab4f8a6183 |
Hashes for polars_pairing-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fd48c3ca875fdaf45bef96588f964857ba74c9ba2d2f1d85f9e44efb5a1e184 |
|
MD5 | c3fb44597e52728a0ef44460eeeb1ce7 |
|
BLAKE2b-256 | 28dd5ae31749b76a0a7952bf28c052000a22e9a77169d2fa1dda62f8da960356 |
Hashes for polars_pairing-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5a0cb5c2c791c0e84a62992d04277dd00746b759ddf1e6421a639ea8bd780f8 |
|
MD5 | e74f0926c98ec1a11ee8d385fb720a20 |
|
BLAKE2b-256 | f7a739b76860642ae2ed612b70daca49206657a53656fc1cda5073370e4613b4 |