Skip to content

roundp

laktory.polars.expressions.roundp ยค

roundp(x, p=1.0)

Evenly round to the given precision

PARAMETER DESCRIPTION
x

Input column

TYPE: Expr

p

Precision

TYPE: Union[float, Expr] DEFAULT: 1.0

RETURNS DESCRIPTION
Expr

Output column

Examples:

import laktory  # noqa: F401
import polars as pl

df = pl.DataFrame([[0.781], [13.0]], ["x"])
df = df.with_columns(y=pl.Expr.laktory.roundp(pl.col("x"), p=5))
print(df.glimpse(return_as_string=True))
'''
Rows: 2
Columns: 2
$ x <f64> 0.781, 13.0
$ y <f64> 0.0, 15.0
'''

df = df.with_columns(y=pl.Expr.laktory.roundp(pl.col("x"), p=0.25))
print(df.glimpse(return_as_string=True))
'''
Rows: 2
Columns: 2
$ x <f64> 0.781, 13.0
$ y <f64> 0.75, 13.0
'''
Source code in laktory/polars/expressions/math.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def roundp(
    x: pl.Expr,
    p: Union[float, pl.Expr] = 1.0,
) -> pl.Expr:
    """
    Evenly round to the given precision

    Parameters
    ------
    x:
        Input column
    p:
        Precision

    Returns
    -------
    :
        Output column

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

    df = pl.DataFrame([[0.781], [13.0]], ["x"])
    df = df.with_columns(y=pl.Expr.laktory.roundp(pl.col("x"), p=5))
    print(df.glimpse(return_as_string=True))
    '''
    Rows: 2
    Columns: 2
    $ x <f64> 0.781, 13.0
    $ y <f64> 0.0, 15.0
    '''

    df = df.with_columns(y=pl.Expr.laktory.roundp(pl.col("x"), p=0.25))
    print(df.glimpse(return_as_string=True))
    '''
    Rows: 2
    Columns: 2
    $ x <f64> 0.781, 13.0
    $ y <f64> 0.75, 13.0
    '''
    ```
    """
    return (x / p).round() * p