polars-pairing
This plugin provides pairing functions to Polars. These functions encode a pair of natural numbers into a single natural number.
Installation
pip install polars-pairing
Functionality and usage
This plugin implements six pairing functions (Hagen1, Szudzik2, Cantor3, Peter4, Rosenberg-Strong5, and Thotakura6), along with their corresponding unpairing functions, as Polars expressions. The pairing functions take two nonnegative 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("a").pairing.pair(plp.col("b")).alias("pair"))
shape: (4, 1)
┌──────┐
│ pair │
│ --- │
│ u64 │
╞══════╡
│ 28 │
│ 40 │
│ 6006 │
│ 6408 │
└──────┘
# 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"),
... plp.col("a").pairing.pair(plp.col("b"), method="peter").alias("peter_pair"),
... plp.col("a").pairing.pair(plp.col("b"), method="rosenberg_strong").alias("rosenberg_strong_pair"),
... plp.col("a").pairing.pair(plp.col("b"), method="thotakura").alias("thotakura_pair")
... )
shape: (4, 6)
┌────────────┬──────────────┬─────────────┬────────────┬───────────────────────┬────────────────┐
│ hagen_pair ┆ szudzik_pair ┆ cantor_pair ┆ peter_pair ┆ rosenberg_strong_pair ┆ thotakura_pair │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u64 ┆ u64 ┆ u64 ┆ u64 ┆ u64 ┆ u64 │
╞════════════╪══════════════╪═════════════╪════════════╪═══════════════════════╪════════════════╡
│ 28 ┆ 26 ┆ 26 ┆ 28 ┆ 26 ┆ 24 │
│ 40 ┆ 38 ┆ 42 ┆ 41 ┆ 38 ┆ 34 │
│ 6006 ┆ 5967 ┆ 6747 ┆ 6006 ┆ 5967 ┆ 3194 │
│ 6408 ┆ 6404 ┆ 3650 ┆ 6409 ┆ 6404 ┆ 3368 │
└────────────┴──────────────┴─────────────┴────────────┴───────────────────────┴────────────────┘
# 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 │
└──────┴───────┘
>>> df = pl.DataFrame({"p": [28, 41, 6006, 6409]})
>>> df.select(
... plp.col("p").pairing.unpair(method="peter").alias("unpair")
... ).unnest("unpair")
shape: (4, 2)
┌──────┬───────┐
│ Left ┆ Right │
│ --- ┆ --- │
│ u64 ┆ u64 │
╞══════╪═══════╡
│ 1 ┆ 5 │
│ 2 ┆ 6 │
│ 38 ┆ 77 │
│ 4 ┆ 80 │
└──────┴───────┘
>>> df = pl.DataFrame({"p": [26, 38, 5967, 6404]})
>>> df.select(
... plp.col("p").pairing.unpair(method="rosenberg_strong").alias("unpair")
... ).unnest("unpair")
shape: (4, 2)
┌──────┬───────┐
│ Left ┆ Right │
│ --- ┆ --- │
│ u64 ┆ u64 │
╞══════╪═══════╡
│ 1 ┆ 5 │
│ 2 ┆ 6 │
│ 38 ┆ 77 │
│ 4 ┆ 80 │
└──────┴───────┘
>>> df = pl.DataFrame({"p": [24, 34, 3194, 3368]})
>>> df.select(
... plp.col("p").pairing.unpair(method="thotakura").alias("unpair")
... ).unnest("unpair")
shape: (4, 2)
┌──────┬───────┐
│ Left ┆ Right │
│ --- ┆ --- │
│ u64 ┆ u64 │
╞══════╪═══════╡
│ 1 ┆ 5 │
│ 2 ┆ 6 │
│ 38 ┆ 77 │
│ 4 ┆ 80 │
└──────┴───────┘
-
Hagen, D. R. Superior Pairing Function. https://drhagen.com/blog/superior-pairing-function/ (2018). ↩
-
Szudzik, Matthew. "An elegant pairing function." Wolfram Research (ed.) Special NKS 2006 Wolfram Science Conference. 2006. ↩
-
Cantor, G. Ein Beitrag zur Mannigfaltigkeitslehre. Journal für die reine und angewandte Mathematik 1878, 242–258 (1878). ↩
-
Péter, R. Rekursive Funktionen. Akadémiai Kiadó, Budapest (1951). ↩
-
Rosenberg, A. L., and Strong, H. R. "Addressing arrays by shells." IBM Technical Disclosure Bulletin 14 (10), 3026–3028 (1972). ↩
-
Thotakura, Manideep. "A New Bijective Pairing Alternative for Encoding Natural Numbers." Cryptology ePrint Archive (2025). https://eprint.iacr.org/2025/1244. ↩
Metadata
Release files for polars-pairing 0.2.0
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_pairing-0.2.0.tar.gz | 60.5 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| polars_pairing-0.2.0-cp311-abi3-win_amd64.whl | CPython 3.11 | abi3 | Windows x86-64 | Details |
| polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.11 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_i686.manylinux2014_i686.whl | CPython 3.11 | abi3 | Linux glibc 2.17+ x86-32 | Details |
| polars_pairing-0.2.0-cp311-abi3-macosx_11_0_arm64.whl | CPython 3.11 | abi3 | macOS 11.0+ ARM64 | Details |
| polars_pairing-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl | CPython 3.11 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 32.3 MB
Release files / polars_pairing-0.2.0.tar.gz
| Download URL | polars_pairing-0.2.0.tar.gz |
|---|---|
| Size | 60.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
457eace625f57d31bb573b158c98bd75ca2fab33d3a181178ae47b6fb675d4de
|
|
BLAKE2b-256 checksum How to use checksums |
07422dc796101d7d3c92f05b63a521d80d7bcefeb97d2315c01d081df28ad397
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / polars_pairing-0.2.0-cp311-abi3-win_amd64.whl
| Download URL | polars_pairing-0.2.0-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 6.0 MB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
7e1c95da72a1a85b180de3d7bef7c5b9e8dfd545259343d65322e0c185c03298
|
|
BLAKE2b-256 checksum How to use checksums |
7ffa56479f4fc39f3ffffd04da91f38a6a1f3d4bd65c367837b1f8578fa03b66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 6.9 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
4b8f38ec5d59960b563c5ce10aa3f1a8dd1efd137ace0f0649ce50a5b929acd5
|
|
BLAKE2b-256 checksum How to use checksums |
d9db1a1adabcbf880bf022337c84b279479939e48248d63792cb3cc02d65fdda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
| Download URL | polars_pairing-0.2.0-cp311-abi3-manylinux_2_17_i686.manylinux2014_i686.whl |
|---|---|
| Size | 8.1 MB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
5ff587079bfc1b3f86b1fe1a201296fbf60d0dc56b0680c017a34808e76468fd
|
|
BLAKE2b-256 checksum How to use checksums |
50d22f4a185c38b2c5967a40b0538720c76b7bf1a1c392ec1105f902eac1fcb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / polars_pairing-0.2.0-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | polars_pairing-0.2.0-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 5.4 MB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
ceaa24397a2a5f87015d76df45a13c2984c2aa27d9f039b3758489f823b51a8e
|
|
BLAKE2b-256 checksum How to use checksums |
de86179997a134568c8ef41199268b1b46006a17202a4f794927cdeda1bc3297
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / polars_pairing-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl
| Download URL | polars_pairing-0.2.0-cp311-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 5.8 MB |
| Tags | CPython 3.11 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
ca63aeecc6dcb56addaf5651e14f47d9e9d7da4d2b8a2cab3ddaaf27f319b74b
|
|
BLAKE2b-256 checksum How to use checksums |
089d08209845b55b299aaea0a4f57e02d21854947076dc12b717c5f4ad572b1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|