Skip to content

Convert a String column into a Decimal column

Description

This method infers the needed parameters precision and scale.

Usage

<Expr>$str$to_decimal(..., inference_length = 100)

Arguments

These dots are for future extensions and must be empty.
inference_length Number of elements to parse to determine the precision and scale.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  numbers = c(
    "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())
#> shape: (7, 2)
#> ┌───────────┬─────────────────┐
#> │ numbers   ┆ numbers_decimal │
#> │ ---       ┆ ---             │
#> │ str       ┆ decimal[*,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          │
#> └───────────┴─────────────────┘