Skip to content

Convert a String column into an Int64 column with base radix

Source code

Description

Convert a String column into an Int64 column with base radix

Usage

<Expr>$str$to_integer(..., base = 10L, strict = TRUE)

Arguments

Ignored.
base A positive integer or expression which is the base of the string we are parsing. Characters are parsed as column names. Default: 10L.
strict A logical. If TRUE (default), parsing errors or integer overflow will raise an error. If FALSE, silently convert to null.

Value

Expression of data type Int64.

Examples

library("polars")

df = pl$DataFrame(bin = c("110", "101", "010", "invalid"))
df$with_columns(
  parsed = pl$col("bin")$str$to_integer(base = 2, strict = FALSE)
)
#> shape: (4, 2)
#> ┌─────────┬────────┐
#> │ bin     ┆ parsed │
#> │ ---     ┆ ---    │
#> │ str     ┆ i64    │
#> ╞═════════╪════════╡
#> │ 110     ┆ 6      │
#> │ 101     ┆ 5      │
#> │ 010     ┆ 2      │
#> │ invalid ┆ null   │
#> └─────────┴────────┘
df = pl$DataFrame(hex = c("fa1e", "ff00", "cafe", NA))
df$with_columns(
  parsed = pl$col("hex")$str$to_integer(base = 16, strict = TRUE)
)
#> shape: (4, 2)
#> ┌──────┬────────┐
#> │ hex  ┆ parsed │
#> │ ---  ┆ ---    │
#> │ str  ┆ i64    │
#> ╞══════╪════════╡
#> │ fa1e ┆ 64030  │
#> │ ff00 ┆ 65280  │
#> │ cafe ┆ 51966  │
#> │ null ┆ null   │
#> └──────┴────────┘