polars.Expr.str.to_decimal#

Expr.str.to_decimal(*, scale: int) Expr[source]#

Convert a String column into a Decimal column.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Changed in version 1.20.0: Parameter inference_length should now be passed as a keyword argument.

Changed in version 1.33.0: Parameter inference_length was removed and scale was made non-optional.

Parameters:
scale

Number of digits after the comma to use for the decimals.

Examples

>>> df = pl.DataFrame(
...     {
...         "numbers": [
...             "40.12",
...             "3420.13",
...             "120134.19",
...             "3212.98",
...             "12.90",
...             "143.09",
...             "143.9",
...         ]
...     }
... )
>>> df.with_columns(numbers_decimal=pl.col("numbers").str.to_decimal(scale=2))
shape: (7, 2)
┌───────────┬─────────────────┐
│ numbers   ┆ numbers_decimal │
│ ---       ┆ ---             │
│ str       ┆ decimal[38,2]   │
╞═══════════╪═════════════════╡
│ 40.12     ┆ 40.12           │
│ 3420.13   ┆ 3420.13         │
│ 120134.19 ┆ 120134.19       │
│ 3212.98   ┆ 3212.98         │
│ 12.90     ┆ 12.90           │
│ 143.09    ┆ 143.09          │
│ 143.9     ┆ 143.90          │
└───────────┴─────────────────┘