Skip to content

Decode a value using the provided encoding

Description

Decode a value using the provided encoding

Usage

<Expr>$str$decode(encoding, ..., strict = TRUE)

Arguments

encoding Either ‘hex’ or ‘base64’.
These dots are for future extensions and must be empty.
strict If TRUE (default), raise an error if the underlying value cannot be decoded. Otherwise, replace it with a null value.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(strings = c("foo", "bar", NA))
df$select(pl$col("strings")$str$encode("hex"))
#> shape: (3, 1)
#> ┌─────────┐
#> │ strings │
#> │ ---     │
#> │ str     │
#> ╞═════════╡
#> │ 666f6f  │
#> │ 626172  │
#> │ null    │
#> └─────────┘
df$with_columns(
  pl$col("strings")$str$encode("base64")$alias("base64"), # notice DataType is not encoded
  pl$col("strings")$str$encode("hex")$alias("hex") # ... and must restored with cast
)$with_columns(
  pl$col("base64")$str$decode("base64")$alias("base64_decoded")$cast(pl$String),
  pl$col("hex")$str$decode("hex")$alias("hex_decoded")$cast(pl$String)
)
#> shape: (3, 5)
#> ┌─────────┬────────┬────────┬────────────────┬─────────────┐
#> │ strings ┆ base64 ┆ hex    ┆ base64_decoded ┆ hex_decoded │
#> │ ---     ┆ ---    ┆ ---    ┆ ---            ┆ ---         │
#> │ str     ┆ str    ┆ str    ┆ str            ┆ str         │
#> ╞═════════╪════════╪════════╪════════════════╪═════════════╡
#> │ foo     ┆ Zm9v   ┆ 666f6f ┆ foo            ┆ foo         │
#> │ bar     ┆ YmFy   ┆ 626172 ┆ bar            ┆ bar         │
#> │ null    ┆ null   ┆ null   ┆ null           ┆ null        │
#> └─────────┴────────┴────────┴────────────────┴─────────────┘