Skip to content

uuid

laktory.polars.expressions.uuid ยค

uuid()

Create a unique id for each row.

RETURNS DESCRIPTION
Expr

Output column

Examples:

import laktory  # noqa: F401
import polars as pl

df = pl.DataFrame({"id": [0, 1, 2]})
df = df.with_columns(uuid=pl.Expr.laktory.uuid())
Source code in laktory/polars/expressions/string.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def uuid() -> pl.Expr:
    """
    Create a unique id for each row.

    Returns
    -------
    :
        Output column

    Examples
    --------
    ```py
    import laktory  # noqa: F401
    import polars as pl

    df = pl.DataFrame({"id": [0, 1, 2]})
    df = df.with_columns(uuid=pl.Expr.laktory.uuid())
    ```
    """

    def generate_uuid():
        import uuid

        return str(uuid.uuid4())

    return pl.first().map_elements(lambda _: generate_uuid(), return_dtype=pl.Utf8)