Skip to content

roundp

laktory.spark.functions.roundp ยค

roundp(x, p=1.0)

Evenly round to the given precision

PARAMETER DESCRIPTION
x

Input column

TYPE: COLUMN_OR_NAME

p

Precision

TYPE: FLOAT_OR_COLUMN DEFAULT: 1.0

RETURNS DESCRIPTION
Column

Output column

Examples:

import laktory  # noqa: F401
import pyspark.sql.functions as F

df = spark.createDataFrame([[0.781], [13.0]], ["x"])
df = df.withColumn("y", F.laktory.roundp("x", p=5))
print(df.laktory.show_string())
'''
+-----+----+
|    x|   y|
+-----+----+
|0.781| 0.0|
| 13.0|15.0|
+-----+----+
'''

df = df.withColumn("y", F.laktory.roundp("x", p=0.25))
print(df.laktory.show_string())
'''
+-----+----+
|    x|   y|
+-----+----+
|0.781|0.75|
| 13.0|13.0|
+-----+----+
'''
Source code in laktory/spark/functions/math.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def roundp(
    x: COLUMN_OR_NAME,
    p: FLOAT_OR_COLUMN = 1.0,
) -> Column:
    """
    Evenly round to the given precision

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

    Returns
    -------
    :
        Output column

    Examples
    --------
    ```py
    import laktory  # noqa: F401
    import pyspark.sql.functions as F

    df = spark.createDataFrame([[0.781], [13.0]], ["x"])
    df = df.withColumn("y", F.laktory.roundp("x", p=5))
    print(df.laktory.show_string())
    '''
    +-----+----+
    |    x|   y|
    +-----+----+
    |0.781| 0.0|
    | 13.0|15.0|
    +-----+----+
    '''

    df = df.withColumn("y", F.laktory.roundp("x", p=0.25))
    print(df.laktory.show_string())
    '''
    +-----+----+
    |    x|   y|
    +-----+----+
    |0.781|0.75|
    | 13.0|13.0|
    +-----+----+
    '''
    ```
    """
    # eps0 = 1.0e-16
    # precision = float(precision)
    # if precision < eps0:
    #     raise ValueError("Precision must be greater than 1.0e-16")
    return F.round(_col(x) / _lit(p)) * _lit(p)