polars.Expr.truncate#
- Expr.truncate(decimals: int = 0) Expr[source]#
Truncate numeric data toward zero to
decimalsnumber of decimal places.- Parameters:
- decimals
Number of decimal places to truncate to.
See also
ceilRound up to the nearest integer.
floorRound down to the nearest integer.
roundRound to a given number of decimals.
round_sig_figsRound to a given number of significant figures.
Notes
Truncation discards the fractional part beyond the given number of decimals. For example, when rounding to 0 decimals 0.25, -0.25, 0.99, and -0.99 will all round to 0. When rounding to 1 decimal 1.9999 rounds to 1.9 and -1.9999 rounds to -1.9. There is no tiebreak behaviour at midpoint values as there is with
round()so 0.5 and -0.5 will also round to 0 when decimals=1.This method performs numeric truncation. For truncating temporal data (dates/datetimes), use
Expr.dt.truncate()instead.
Examples
>>> df = pl.DataFrame({"n": [-9.9999, 0.12345, 1.0251, 8.8765]}) >>> df.with_columns( ... t0=pl.col("n").truncate(0), ... t1=pl.col("n").truncate(1), ... t2=pl.col("n").truncate(2), ... t3=pl.col("n").truncate(3), ... t4=pl.col("n").truncate(4), ... ) shape: (4, 6) ┌─────────┬──────┬──────┬───────┬────────┬─────────┐ │ n ┆ t0 ┆ t1 ┆ t2 ┆ t3 ┆ t4 │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞═════════╪══════╪══════╪═══════╪════════╪═════════╡ │ -9.9999 ┆ -9.0 ┆ -9.9 ┆ -9.99 ┆ -9.999 ┆ -9.9999 │ │ 0.12345 ┆ 0.0 ┆ 0.1 ┆ 0.12 ┆ 0.123 ┆ 0.1234 │ │ 1.0251 ┆ 1.0 ┆ 1.0 ┆ 1.02 ┆ 1.025 ┆ 1.025 │ │ 8.8765 ┆ 8.0 ┆ 8.8 ┆ 8.87 ┆ 8.876 ┆ 8.8765 │ └─────────┴──────┴──────┴───────┴────────┴─────────┘