Skip to content

Scale

toolbox_pyspark.scale 🔗

Summary

The scale module is used for rounding a column (or columns) to a given rounding accuracy.

round_column 🔗

round_column(
    dataframe: psDataFrame,
    column: str,
    scale: int = DEFAULT_DECIMAL_ACCURACY,
) -> psDataFrame

Summary

For a given dataframe, on a given column if the column data type is decimal (that is, one of: ["float", "double", "decimal"]), then round that column to a scale accuracy at a given number of decimal places.

Details

Realistically, under the hood, this function is super simple. It merely runs:

Python
1
dataframe = dataframe.withColumn(colName=column, col=F.round(col=column, scale=scale))
This function merely adds some additional validation, and is enabled to run in a pyspark .transform() method. For more info, see: pyspark.sql.DataFrame.transform

Parameters:

Name Type Description Default
dataframe DataFrame

The dataframe to be transformed.

required
column str

The desired column to be rounded.

required
scale int

The required level of rounding for the column.
If not provided explicitly, it will default to the global value DEFAULT_DECIMAL_ACCURACY; which is 10.
Defaults to DEFAULT_DECIMAL_ACCURACY.

DEFAULT_DECIMAL_ACCURACY

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

TypeError

If the given column is not one of the correct data types for rounding. It must be one of: ["float", "double", "decimal"].

Returns:

Type Description
DataFrame

The transformed dataframe containing the column which has now been rounded.

Examples

Set up
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession, functions as F, types as T
>>> from toolbox_pyspark.io import read_from_path
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = (
...     spark
...     .createDataFrame(
...         pd.DataFrame(
...             {
...                 "a": range(20),
...                 "b": [f"1.{'0'*val}1" for val in range(20)],
...                 "c": [f"1.{'0'*val}6" for val in range(20)],
...             }
...         )
...     )
...     .withColumns(
...         {
...             "b": F.col("b").cast(T.DecimalType(21, 20)),
...             "c": F.col("c").cast(T.DecimalType(21, 20)),
...         }
...     )
... )
>>>
>>> # Check
>>> df.show(truncate=False)
Terminal
+---+----------------------+----------------------+
|a  |b                     |c                     |
+---+----------------------+----------------------+
|0  |1.10000000000000000000|1.60000000000000000000|
|1  |1.01000000000000000000|1.06000000000000000000|
|2  |1.00100000000000000000|1.00600000000000000000|
|3  |1.00010000000000000000|1.00060000000000000000|
|4  |1.00001000000000000000|1.00006000000000000000|
|5  |1.00000100000000000000|1.00000600000000000000|
|6  |1.00000010000000000000|1.00000060000000000000|
|7  |1.00000001000000000000|1.00000006000000000000|
|8  |1.00000000100000000000|1.00000000600000000000|
|9  |1.00000000010000000000|1.00000000060000000000|
|10 |1.00000000001000000000|1.00000000006000000000|
|11 |1.00000000000100000000|1.00000000000600000000|
|12 |1.00000000000010000000|1.00000000000060000000|
|13 |1.00000000000001000000|1.00000000000006000000|
|14 |1.00000000000000100000|1.00000000000000600000|
|15 |1.00000000000000010000|1.00000000000000060000|
|16 |1.00000000000000001000|1.00000000000000006000|
|17 |1.00000000000000000100|1.00000000000000000600|
|18 |1.00000000000000000010|1.00000000000000000060|
|19 |1.00000000000000000001|1.00000000000000000006|
+---+----------------------+----------------------+

Example 1: Round with defaults
1
>>> round_column(df, "b").show(truncate=False)
Terminal
+---+------------+----------------------+
|a  |b           |c                     |
+---+------------+----------------------+
|0  |1.1000000000|1.60000000000000000000|
|1  |1.0100000000|1.06000000000000000000|
|2  |1.0010000000|1.00600000000000000000|
|3  |1.0001000000|1.00060000000000000000|
|4  |1.0000100000|1.00006000000000000000|
|5  |1.0000010000|1.00000600000000000000|
|6  |1.0000001000|1.00000060000000000000|
|7  |1.0000000100|1.00000006000000000000|
|8  |1.0000000010|1.00000000600000000000|
|9  |1.0000000001|1.00000000060000000000|
|10 |1.0000000000|1.00000000006000000000|
|11 |1.0000000000|1.00000000000600000000|
|12 |1.0000000000|1.00000000000060000000|
|13 |1.0000000000|1.00000000000006000000|
|14 |1.0000000000|1.00000000000000600000|
|15 |1.0000000000|1.00000000000000060000|
|16 |1.0000000000|1.00000000000000006000|
|17 |1.0000000000|1.00000000000000000600|
|18 |1.0000000000|1.00000000000000000060|
|19 |1.0000000000|1.00000000000000000006|
+---+------------+----------------------+

Conclusion: Successfully rounded column b.

Example 2: Round to custom number
1
>>> round_column(df, "c", 5).show(truncate=False)
Terminal
+---+----------------------+-------+
|a  |b                     |c      |
+---+----------------------+-------+
|0  |1.10000000000000000000|1.60000|
|1  |1.01000000000000000000|1.06000|
|2  |1.00100000000000000000|1.00600|
|3  |1.00010000000000000000|1.00060|
|4  |1.00001000000000000000|1.00006|
|5  |1.00000100000000000000|1.00001|
|6  |1.00000010000000000000|1.00000|
|7  |1.00000001000000000000|1.00000|
|8  |1.00000000100000000000|1.00000|
|9  |1.00000000010000000000|1.00000|
|10 |1.00000000001000000000|1.00000|
|11 |1.00000000000100000000|1.00000|
|12 |1.00000000000010000000|1.00000|
|13 |1.00000000000001000000|1.00000|
|14 |1.00000000000000100000|1.00000|
|15 |1.00000000000000010000|1.00000|
|16 |1.00000000000000001000|1.00000|
|17 |1.00000000000000000100|1.00000|
|18 |1.00000000000000000010|1.00000|
|19 |1.00000000000000000001|1.00000|
+---+----------------------+-------+

Conclusion: Successfully rounded column b to 5 decimal points.

Example 3: Raise error
1
>>> round_column(df, "a").show(truncate=False)
Terminal
TypeError: Column is not the correct type. Please check.
For column 'a', the type is 'bigint'.
In order to round it, it needs to be one of: '["float", "double", "decimal"]'.

Conclusion: Cannot round a column a.

Source code in src/toolbox_pyspark/scale.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
@typechecked
def round_column(
    dataframe: psDataFrame,
    column: str,
    scale: int = DEFAULT_DECIMAL_ACCURACY,
) -> psDataFrame:
    """
    !!! note "Summary"
        For a given `dataframe`, on a given `column` if the column data type is decimal (that is, one of: `#!py ["float", "double", "decimal"]`), then round that column to a `scale` accuracy at a given number of decimal places.

    ???+ abstract "Details"
        Realistically, under the hood, this function is super simple. It merely runs:
        ```{.py .python linenums="1" title="Python"}
        dataframe = dataframe.withColumn(colName=column, col=F.round(col=column, scale=scale))
        ```
        This function merely adds some additional validation, and is enabled to run in a pyspark `.transform()` method.
        For more info, see: [`pyspark.sql.DataFrame.transform`](https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.DataFrame.transform.html)

    Params:
        dataframe (psDataFrame):
            The `dataframe` to be transformed.
        column (str):
            The desired column to be rounded.
        scale (int, optional):
            The required level of rounding for the column.<br>
            If not provided explicitly, it will default to the global value `#!py DEFAULT_DECIMAL_ACCURACY`; which is `#!py 10`.<br>
            Defaults to `#!py DEFAULT_DECIMAL_ACCURACY`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        TypeError:
            If the given `column` is not one of the correct data types for rounding. It must be one of: `#!py ["float", "double", "decimal"]`.

    Returns:
        (psDataFrame):
            The transformed `dataframe` containing the column which has now been rounded.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession, functions as F, types as T
        >>> from toolbox_pyspark.io import read_from_path
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = (
        ...     spark
        ...     .createDataFrame(
        ...         pd.DataFrame(
        ...             {
        ...                 "a": range(20),
        ...                 "b": [f"1.{'0'*val}1" for val in range(20)],
        ...                 "c": [f"1.{'0'*val}6" for val in range(20)],
        ...             }
        ...         )
        ...     )
        ...     .withColumns(
        ...         {
        ...             "b": F.col("b").cast(T.DecimalType(21, 20)),
        ...             "c": F.col("c").cast(T.DecimalType(21, 20)),
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+----------------------+----------------------+
        |a  |b                     |c                     |
        +---+----------------------+----------------------+
        |0  |1.10000000000000000000|1.60000000000000000000|
        |1  |1.01000000000000000000|1.06000000000000000000|
        |2  |1.00100000000000000000|1.00600000000000000000|
        |3  |1.00010000000000000000|1.00060000000000000000|
        |4  |1.00001000000000000000|1.00006000000000000000|
        |5  |1.00000100000000000000|1.00000600000000000000|
        |6  |1.00000010000000000000|1.00000060000000000000|
        |7  |1.00000001000000000000|1.00000006000000000000|
        |8  |1.00000000100000000000|1.00000000600000000000|
        |9  |1.00000000010000000000|1.00000000060000000000|
        |10 |1.00000000001000000000|1.00000000006000000000|
        |11 |1.00000000000100000000|1.00000000000600000000|
        |12 |1.00000000000010000000|1.00000000000060000000|
        |13 |1.00000000000001000000|1.00000000000006000000|
        |14 |1.00000000000000100000|1.00000000000000600000|
        |15 |1.00000000000000010000|1.00000000000000060000|
        |16 |1.00000000000000001000|1.00000000000000006000|
        |17 |1.00000000000000000100|1.00000000000000000600|
        |18 |1.00000000000000000010|1.00000000000000000060|
        |19 |1.00000000000000000001|1.00000000000000000006|
        +---+----------------------+----------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Round with defaults"}
        >>> round_column(df, "b").show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+------------+----------------------+
        |a  |b           |c                     |
        +---+------------+----------------------+
        |0  |1.1000000000|1.60000000000000000000|
        |1  |1.0100000000|1.06000000000000000000|
        |2  |1.0010000000|1.00600000000000000000|
        |3  |1.0001000000|1.00060000000000000000|
        |4  |1.0000100000|1.00006000000000000000|
        |5  |1.0000010000|1.00000600000000000000|
        |6  |1.0000001000|1.00000060000000000000|
        |7  |1.0000000100|1.00000006000000000000|
        |8  |1.0000000010|1.00000000600000000000|
        |9  |1.0000000001|1.00000000060000000000|
        |10 |1.0000000000|1.00000000006000000000|
        |11 |1.0000000000|1.00000000000600000000|
        |12 |1.0000000000|1.00000000000060000000|
        |13 |1.0000000000|1.00000000000006000000|
        |14 |1.0000000000|1.00000000000000600000|
        |15 |1.0000000000|1.00000000000000060000|
        |16 |1.0000000000|1.00000000000000006000|
        |17 |1.0000000000|1.00000000000000000600|
        |18 |1.0000000000|1.00000000000000000060|
        |19 |1.0000000000|1.00000000000000000006|
        +---+------------+----------------------+
        ```
        !!! success "Conclusion: Successfully rounded column `b`."
        </div>

        ```{.py .python linenums="1" title="Example 2: Round to custom number"}
        >>> round_column(df, "c", 5).show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+----------------------+-------+
        |a  |b                     |c      |
        +---+----------------------+-------+
        |0  |1.10000000000000000000|1.60000|
        |1  |1.01000000000000000000|1.06000|
        |2  |1.00100000000000000000|1.00600|
        |3  |1.00010000000000000000|1.00060|
        |4  |1.00001000000000000000|1.00006|
        |5  |1.00000100000000000000|1.00001|
        |6  |1.00000010000000000000|1.00000|
        |7  |1.00000001000000000000|1.00000|
        |8  |1.00000000100000000000|1.00000|
        |9  |1.00000000010000000000|1.00000|
        |10 |1.00000000001000000000|1.00000|
        |11 |1.00000000000100000000|1.00000|
        |12 |1.00000000000010000000|1.00000|
        |13 |1.00000000000001000000|1.00000|
        |14 |1.00000000000000100000|1.00000|
        |15 |1.00000000000000010000|1.00000|
        |16 |1.00000000000000001000|1.00000|
        |17 |1.00000000000000000100|1.00000|
        |18 |1.00000000000000000010|1.00000|
        |19 |1.00000000000000000001|1.00000|
        +---+----------------------+-------+
        ```
        !!! success "Conclusion: Successfully rounded column `b` to 5 decimal points."
        </div>

        ```{.py .python linenums="1" title="Example 3: Raise error"}
        >>> round_column(df, "a").show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        TypeError: Column is not the correct type. Please check.
        For column 'a', the type is 'bigint'.
        In order to round it, it needs to be one of: '["float", "double", "decimal"]'.
        ```
        !!! failure "Conclusion: Cannot round a column `a`."
        </div>
    """
    assert_column_exists(dataframe, column)
    col_type: str = [typ.split("(")[0] for col, typ in dataframe.dtypes if col == column][0]
    if col_type not in VALID_TYPES:
        raise TypeError(
            f"Column is not the correct type. Please check.\n"
            f"For column '{column}', the type is '{col_type}'.\n"
            f"In order to round it, it needs to be one of: '{VALID_TYPES}'."
        )
    return dataframe.withColumn(colName=column, col=F.round(col=column, scale=scale))

round_columns 🔗

round_columns(
    dataframe: psDataFrame,
    columns: Optional[
        Union[str, str_collection]
    ] = "all_float",
    scale: int = DEFAULT_DECIMAL_ACCURACY,
) -> psDataFrame

Summary

For a given dataframe, on a set of columns if the column data type is decimal (that is, one of: ["float", "double", "decimal"]), then round that column to a scale accuracy at a given number of decimal places.

Details

Realistically, under the hood, this function is super simple. It merely runs:

Python
1
dataframe = dataframe.withColumns({col: F.round(col, scale) for col in columns})
This function merely adds some additional validation, and is enabled to run in a pyspark .transform() method. For more info, see: pyspark.sql.DataFrame.transform

Parameters:

Name Type Description Default
dataframe DataFrame

The dataframe to be transformed.

required
columns Optional[Union[str, str_collection]]

The desired column to be rounded.
If no value is parsed, or is the value None, or one of ["all", "all_float"], then it will default to all numeric decimal columns on the dataframe.
If the value is a str, then it will be coerced to a single-element list, like: [columns].
Defaults to "all_float".

'all_float'
scale int

The required level of rounding for the column.
If not provided explicitly, it will default to the global value DEFAULT_DECIMAL_ACCURACY; which is 10.
Defaults to DEFAULT_DECIMAL_ACCURACY.

DEFAULT_DECIMAL_ACCURACY

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

TypeError

If any of the given columns are not one of the correct data types for rounding. They must be one of: ["float", "double", "decimal"].

Returns:

Type Description
DataFrame

The transformed dataframe containing the column which has now been rounded.

Examples

Set up
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession, functions as F, types as T
>>> from toolbox_pyspark.io import read_from_path
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = (
...     spark
...     .createDataFrame(
...         pd.DataFrame(
...             {
...                 "a": range(20),
...                 "b": [f"1.{'0'*val}1" for val in range(20)],
...                 "c": [f"1.{'0'*val}6" for val in range(20)],
...             }
...         )
...     )
...     .withColumns(
...         {
...             "b": F.col("b").cast(T.DecimalType(21, 20)),
...             "c": F.col("c").cast(T.DecimalType(21, 20)),
...         }
...     )
... )
>>>
>>> # Check
>>> df.show(truncate=False)
Terminal
+---+----------------------+----------------------+
|a  |b                     |c                     |
+---+----------------------+----------------------+
|0  |1.10000000000000000000|1.60000000000000000000|
|1  |1.01000000000000000000|1.06000000000000000000|
|2  |1.00100000000000000000|1.00600000000000000000|
|3  |1.00010000000000000000|1.00060000000000000000|
|4  |1.00001000000000000000|1.00006000000000000000|
|5  |1.00000100000000000000|1.00000600000000000000|
|6  |1.00000010000000000000|1.00000060000000000000|
|7  |1.00000001000000000000|1.00000006000000000000|
|8  |1.00000000100000000000|1.00000000600000000000|
|9  |1.00000000010000000000|1.00000000060000000000|
|10 |1.00000000001000000000|1.00000000006000000000|
|11 |1.00000000000100000000|1.00000000000600000000|
|12 |1.00000000000010000000|1.00000000000060000000|
|13 |1.00000000000001000000|1.00000000000006000000|
|14 |1.00000000000000100000|1.00000000000000600000|
|15 |1.00000000000000010000|1.00000000000000060000|
|16 |1.00000000000000001000|1.00000000000000006000|
|17 |1.00000000000000000100|1.00000000000000000600|
|18 |1.00000000000000000010|1.00000000000000000060|
|19 |1.00000000000000000001|1.00000000000000000006|
+---+----------------------+----------------------+

Example 1: Round with defaults
1
>>> round_columns(df).show(truncate=False)
Terminal
+---+------------+------------+
|  a|           b|           c|
+---+------------+------------+
|  0|1.1000000000|1.6000000000|
|  1|1.0100000000|1.0600000000|
|  2|1.0010000000|1.0060000000|
|  3|1.0001000000|1.0006000000|
|  4|1.0000100000|1.0000600000|
|  5|1.0000010000|1.0000060000|
|  6|1.0000001000|1.0000006000|
|  7|1.0000000100|1.0000000600|
|  8|1.0000000010|1.0000000060|
|  9|1.0000000001|1.0000000006|
| 10|1.0000000000|1.0000000001|
| 11|1.0000000000|1.0000000000|
| 12|1.0000000000|1.0000000000|
| 13|1.0000000000|1.0000000000|
| 14|1.0000000000|1.0000000000|
| 15|1.0000000000|1.0000000000|
| 16|1.0000000000|1.0000000000|
| 17|1.0000000000|1.0000000000|
| 18|1.0000000000|1.0000000000|
| 19|1.0000000000|1.0000000000|
+---+------------+------------+

Example 2: Round to custom number
1
>>> round_columns(df, "c", 5).show(truncate=False)
Terminal
+---+----------------------+-------+
|a  |b                     |c      |
+---+----------------------+-------+
|0  |1.10000000000000000000|1.60000|
|1  |1.01000000000000000000|1.06000|
|2  |1.00100000000000000000|1.00600|
|3  |1.00010000000000000000|1.00060|
|4  |1.00001000000000000000|1.00006|
|5  |1.00000100000000000000|1.00001|
|6  |1.00000010000000000000|1.00000|
|7  |1.00000001000000000000|1.00000|
|8  |1.00000000100000000000|1.00000|
|9  |1.00000000010000000000|1.00000|
|10 |1.00000000001000000000|1.00000|
|11 |1.00000000000100000000|1.00000|
|12 |1.00000000000010000000|1.00000|
|13 |1.00000000000001000000|1.00000|
|14 |1.00000000000000100000|1.00000|
|15 |1.00000000000000010000|1.00000|
|16 |1.00000000000000001000|1.00000|
|17 |1.00000000000000000100|1.00000|
|18 |1.00000000000000000010|1.00000|
|19 |1.00000000000000000001|1.00000|
+---+----------------------+-------+

Example 3: Raise error
1
>>> round_columns(df, ["a", "b"]).show(truncate=False)
Terminal
TypeError: Columns are not the correct types. Please check.
These columns are invalid: '[("a", "bigint")]'.
In order to round them, they need to be one of: '["float", "double", "decimal"]'.

Source code in src/toolbox_pyspark/scale.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
@typechecked
def round_columns(
    dataframe: psDataFrame,
    columns: Optional[Union[str, str_collection]] = "all_float",
    scale: int = DEFAULT_DECIMAL_ACCURACY,
) -> psDataFrame:
    """
    !!! note "Summary"
        For a given `dataframe`, on a set of `columns` if the column data type is decimal (that is, one of: `#!py ["float", "double", "decimal"]`), then round that column to a `scale` accuracy at a given number of decimal places.

    ???+ abstract "Details"
        Realistically, under the hood, this function is super simple. It merely runs:
        ```{.py .python linenums="1" title="Python"}
        dataframe = dataframe.withColumns({col: F.round(col, scale) for col in columns})
        ```
        This function merely adds some additional validation, and is enabled to run in a pyspark `.transform()` method.
        For more info, see: [`pyspark.sql.DataFrame.transform`](https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.DataFrame.transform.html)

    Params:
        dataframe (psDataFrame):
            The `dataframe` to be transformed.
        columns (Optional[Union[str, str_collection]], optional):
            The desired column to be rounded.<br>
            If no value is parsed, or is the value `#!py None`, or one of `#!py ["all", "all_float"]`, then it will default to all numeric decimal columns on the `dataframe`.<br>
            If the value is a `#!py str`, then it will be coerced to a single-element list, like: `#!py [columns]`.<br>
            Defaults to `#!py "all_float"`.
        scale (int, optional):
            The required level of rounding for the column.<br>
            If not provided explicitly, it will default to the global value `#!py DEFAULT_DECIMAL_ACCURACY`; which is `#!py 10`.<br>
            Defaults to `#!py DEFAULT_DECIMAL_ACCURACY`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        TypeError:
            If any of the given `columns` are not one of the correct data types for rounding. They must be one of: `#!py ["float", "double", "decimal"]`.

    Returns:
        (psDataFrame):
            The transformed `dataframe` containing the column which has now been rounded.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession, functions as F, types as T
        >>> from toolbox_pyspark.io import read_from_path
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = (
        ...     spark
        ...     .createDataFrame(
        ...         pd.DataFrame(
        ...             {
        ...                 "a": range(20),
        ...                 "b": [f"1.{'0'*val}1" for val in range(20)],
        ...                 "c": [f"1.{'0'*val}6" for val in range(20)],
        ...             }
        ...         )
        ...     )
        ...     .withColumns(
        ...         {
        ...             "b": F.col("b").cast(T.DecimalType(21, 20)),
        ...             "c": F.col("c").cast(T.DecimalType(21, 20)),
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+----------------------+----------------------+
        |a  |b                     |c                     |
        +---+----------------------+----------------------+
        |0  |1.10000000000000000000|1.60000000000000000000|
        |1  |1.01000000000000000000|1.06000000000000000000|
        |2  |1.00100000000000000000|1.00600000000000000000|
        |3  |1.00010000000000000000|1.00060000000000000000|
        |4  |1.00001000000000000000|1.00006000000000000000|
        |5  |1.00000100000000000000|1.00000600000000000000|
        |6  |1.00000010000000000000|1.00000060000000000000|
        |7  |1.00000001000000000000|1.00000006000000000000|
        |8  |1.00000000100000000000|1.00000000600000000000|
        |9  |1.00000000010000000000|1.00000000060000000000|
        |10 |1.00000000001000000000|1.00000000006000000000|
        |11 |1.00000000000100000000|1.00000000000600000000|
        |12 |1.00000000000010000000|1.00000000000060000000|
        |13 |1.00000000000001000000|1.00000000000006000000|
        |14 |1.00000000000000100000|1.00000000000000600000|
        |15 |1.00000000000000010000|1.00000000000000060000|
        |16 |1.00000000000000001000|1.00000000000000006000|
        |17 |1.00000000000000000100|1.00000000000000000600|
        |18 |1.00000000000000000010|1.00000000000000000060|
        |19 |1.00000000000000000001|1.00000000000000000006|
        +---+----------------------+----------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Round with defaults"}
        >>> round_columns(df).show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+------------+------------+
        |  a|           b|           c|
        +---+------------+------------+
        |  0|1.1000000000|1.6000000000|
        |  1|1.0100000000|1.0600000000|
        |  2|1.0010000000|1.0060000000|
        |  3|1.0001000000|1.0006000000|
        |  4|1.0000100000|1.0000600000|
        |  5|1.0000010000|1.0000060000|
        |  6|1.0000001000|1.0000006000|
        |  7|1.0000000100|1.0000000600|
        |  8|1.0000000010|1.0000000060|
        |  9|1.0000000001|1.0000000006|
        | 10|1.0000000000|1.0000000001|
        | 11|1.0000000000|1.0000000000|
        | 12|1.0000000000|1.0000000000|
        | 13|1.0000000000|1.0000000000|
        | 14|1.0000000000|1.0000000000|
        | 15|1.0000000000|1.0000000000|
        | 16|1.0000000000|1.0000000000|
        | 17|1.0000000000|1.0000000000|
        | 18|1.0000000000|1.0000000000|
        | 19|1.0000000000|1.0000000000|
        +---+------------+------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 2: Round to custom number"}
        >>> round_columns(df, "c", 5).show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+----------------------+-------+
        |a  |b                     |c      |
        +---+----------------------+-------+
        |0  |1.10000000000000000000|1.60000|
        |1  |1.01000000000000000000|1.06000|
        |2  |1.00100000000000000000|1.00600|
        |3  |1.00010000000000000000|1.00060|
        |4  |1.00001000000000000000|1.00006|
        |5  |1.00000100000000000000|1.00001|
        |6  |1.00000010000000000000|1.00000|
        |7  |1.00000001000000000000|1.00000|
        |8  |1.00000000100000000000|1.00000|
        |9  |1.00000000010000000000|1.00000|
        |10 |1.00000000001000000000|1.00000|
        |11 |1.00000000000100000000|1.00000|
        |12 |1.00000000000010000000|1.00000|
        |13 |1.00000000000001000000|1.00000|
        |14 |1.00000000000000100000|1.00000|
        |15 |1.00000000000000010000|1.00000|
        |16 |1.00000000000000001000|1.00000|
        |17 |1.00000000000000000100|1.00000|
        |18 |1.00000000000000000010|1.00000|
        |19 |1.00000000000000000001|1.00000|
        +---+----------------------+-------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 3: Raise error"}
        >>> round_columns(df, ["a", "b"]).show(truncate=False)
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        TypeError: Columns are not the correct types. Please check.
        These columns are invalid: '[("a", "bigint")]'.
        In order to round them, they need to be one of: '["float", "double", "decimal"]'.
        ```
        </div>
    """
    if columns is None or columns in ["all", "all_float"]:
        columns = [col for col, typ in dataframe.dtypes if typ.split("(")[0] in VALID_TYPES]
    elif is_type(columns, str):
        columns = [columns]
    assert_columns_exists(dataframe, columns)
    invalid_cols: list[tuple[str, str]] = [
        (col, typ.split("(")[0])
        for col, typ in dataframe.dtypes
        if col in columns and typ.split("(")[0] not in VALID_TYPES
    ]
    if len(invalid_cols) > 0:
        raise TypeError(
            f"Columns are not the correct types. Please check.\n"
            f"These columns are invalid: '{invalid_cols}'.\n"
            f"In order to round them, they need to be one of: '{VALID_TYPES}'."
        )
    return dataframe.withColumns({col: F.round(col, scale) for col in columns})