Skip to content

Reference

polars_pairing.PairingFunctions

Source code in polars_pairing/__init__.py
@pl.api.register_expr_namespace("pairing")
class PairingFunctions:
    def __init__(self, expr: pl.Expr):
        self._expr = expr

    def pair(
        self, other: IntoExpr, method: Literal["hagen", "szudzik", "cantor"] = "hagen"
    ) -> pl.Expr:
        """
        Applies a pairing function to uniquely encode two nonnegative integers
        into a single nonnegative integer.

        Parameters
        ----------
        method
            The method to use for pairing. Either "hagen", "szudzik" or "cantor",
            defaults to "hagen".

        Examples
        --------
        >>> df = pl.DataFrame(
        ...     {
        ...         "n1": [2, 2, 3, 1501],
        ...         "n2": [-6, 7, 8, 10740],
        ...     }
        ... )
        >>> df.select(pl.col("n1").pairing.pair(pl.col("n2")).alias("pair"))
        shape: (4, 1)
        ┌───────────┐
        │ pair      │
        │ ---       │
        │ u64       │
        ╞═══════════╡
        │ null      │
        │ 54        │
        │ 70        │
        │ 115350602 │
        └───────────┘
        >>> df.select(pl.col("n1").pairing.pair(pl.col("n2"), method="szudzik").alias("pair"))
        shape: (4, 1)
        ┌───────────┐
        │ pair      │
        │ ---       │
        │ u64       │
        ╞═══════════╡
        │ null      │
        │ 51        │
        │ 67        │
        │ 115349101 │
        └───────────┘
        """
        return register_plugin(
            args=[self._expr, other],
            kwargs={"method": method},
            symbol="pair",
            is_elementwise=True,
            lib=lib,
        )

    def unpair(
        self, method: Literal["hagen", "szudzik", "cantor"] = "hagen"
    ) -> pl.Expr:
        """
        Applies an unpairing function to uniquely decode a nonnegative integer
        into two nonnegative integers.

        Parameters
        ----------
        method
            The method to use for unpairing. Either "hagen", "szudzik" or "cantor",
            defaults to "hagen".

        Examples
        --------
        >>> df = pl.DataFrame({"pair": [None, 607, -4, 18871362]})
        >>> df.select(pl.col("pair").pairing.unpair().alias("unpair"))
        shape: (4, 1)
        ┌─────────────┐
        │ unpair      │
        │ ---         │
        │ struct[2]   │
        ╞═════════════╡
        │ {null,null} │
        │ {24,15}     │
        │ {null,null} │
        │ {513,4344}  │
        └─────────────┘
        >>> df.select(pl.col("pair").pairing.unpair(method="szudzik").alias("unpair"))
        shape: (4, 1)
        ┌─────────────┐
        │ unpair      │
        │ ---         │
        │ struct[2]   │
        ╞═════════════╡
        │ {null,null} │
        │ {24,7}      │
        │ {null,null} │
        │ {1026,4344} │
        └─────────────┘
        """
        return register_plugin(
            args=[self._expr],
            kwargs={"method": method},
            symbol="unpair",
            is_elementwise=True,
            lib=lib,
        )

pair(other, method='hagen')

Applies a pairing function to uniquely encode two nonnegative integers into a single nonnegative integer.

Parameters:

Name Type Description Default
method Literal['hagen', 'szudzik', 'cantor']

The method to use for pairing. Either "hagen", "szudzik" or "cantor", defaults to "hagen".

'hagen'

Examples:

>>> df = pl.DataFrame(
...     {
...         "n1": [2, 2, 3, 1501],
...         "n2": [-6, 7, 8, 10740],
...     }
... )
>>> df.select(pl.col("n1").pairing.pair(pl.col("n2")).alias("pair"))
shape: (4, 1)
┌───────────┐
│ pair      │
│ ---       │
│ u64       │
╞═══════════╡
│ null      │
│ 54        │
│ 70        │
│ 115350602 │
└───────────┘
>>> df.select(pl.col("n1").pairing.pair(pl.col("n2"), method="szudzik").alias("pair"))
shape: (4, 1)
┌───────────┐
│ pair      │
│ ---       │
│ u64       │
╞═══════════╡
│ null      │
│ 51        │
│ 67        │
│ 115349101 │
└───────────┘
Source code in polars_pairing/__init__.py
def pair(
    self, other: IntoExpr, method: Literal["hagen", "szudzik", "cantor"] = "hagen"
) -> pl.Expr:
    """
    Applies a pairing function to uniquely encode two nonnegative integers
    into a single nonnegative integer.

    Parameters
    ----------
    method
        The method to use for pairing. Either "hagen", "szudzik" or "cantor",
        defaults to "hagen".

    Examples
    --------
    >>> df = pl.DataFrame(
    ...     {
    ...         "n1": [2, 2, 3, 1501],
    ...         "n2": [-6, 7, 8, 10740],
    ...     }
    ... )
    >>> df.select(pl.col("n1").pairing.pair(pl.col("n2")).alias("pair"))
    shape: (4, 1)
    ┌───────────┐
    │ pair      │
    │ ---       │
    │ u64       │
    ╞═══════════╡
    │ null      │
    │ 54        │
    │ 70        │
    │ 115350602 │
    └───────────┘
    >>> df.select(pl.col("n1").pairing.pair(pl.col("n2"), method="szudzik").alias("pair"))
    shape: (4, 1)
    ┌───────────┐
    │ pair      │
    │ ---       │
    │ u64       │
    ╞═══════════╡
    │ null      │
    │ 51        │
    │ 67        │
    │ 115349101 │
    └───────────┘
    """
    return register_plugin(
        args=[self._expr, other],
        kwargs={"method": method},
        symbol="pair",
        is_elementwise=True,
        lib=lib,
    )

unpair(method='hagen')

Applies an unpairing function to uniquely decode a nonnegative integer into two nonnegative integers.

Parameters:

Name Type Description Default
method Literal['hagen', 'szudzik', 'cantor']

The method to use for unpairing. Either "hagen", "szudzik" or "cantor", defaults to "hagen".

'hagen'

Examples:

>>> df = pl.DataFrame({"pair": [None, 607, -4, 18871362]})
>>> df.select(pl.col("pair").pairing.unpair().alias("unpair"))
shape: (4, 1)
┌─────────────┐
│ unpair      │
│ ---         │
│ struct[2]   │
╞═════════════╡
│ {null,null} │
│ {24,15}     │
│ {null,null} │
│ {513,4344}  │
└─────────────┘
>>> df.select(pl.col("pair").pairing.unpair(method="szudzik").alias("unpair"))
shape: (4, 1)
┌─────────────┐
│ unpair      │
│ ---         │
│ struct[2]   │
╞═════════════╡
│ {null,null} │
│ {24,7}      │
│ {null,null} │
│ {1026,4344} │
└─────────────┘
Source code in polars_pairing/__init__.py
def unpair(
    self, method: Literal["hagen", "szudzik", "cantor"] = "hagen"
) -> pl.Expr:
    """
    Applies an unpairing function to uniquely decode a nonnegative integer
    into two nonnegative integers.

    Parameters
    ----------
    method
        The method to use for unpairing. Either "hagen", "szudzik" or "cantor",
        defaults to "hagen".

    Examples
    --------
    >>> df = pl.DataFrame({"pair": [None, 607, -4, 18871362]})
    >>> df.select(pl.col("pair").pairing.unpair().alias("unpair"))
    shape: (4, 1)
    ┌─────────────┐
    │ unpair      │
    │ ---         │
    │ struct[2]   │
    ╞═════════════╡
    │ {null,null} │
    │ {24,15}     │
    │ {null,null} │
    │ {513,4344}  │
    └─────────────┘
    >>> df.select(pl.col("pair").pairing.unpair(method="szudzik").alias("unpair"))
    shape: (4, 1)
    ┌─────────────┐
    │ unpair      │
    │ ---         │
    │ struct[2]   │
    ╞═════════════╡
    │ {null,null} │
    │ {24,7}      │
    │ {null,null} │
    │ {1026,4344} │
    └─────────────┘
    """
    return register_plugin(
        args=[self._expr],
        kwargs={"method": method},
        symbol="unpair",
        is_elementwise=True,
        lib=lib,
    )