Skip to content

Encode a value using the provided encoding

Source code

Description

Encode a value using the provided encoding

Usage

<Expr>$bin$encode(encoding)

Arguments

encoding A character, “hex” or “base64”. The encoding to use.

Value

Expr of data type String.

Examples

library("polars")

df = pl$DataFrame(
  name = c("black", "yellow", "blue"),
  code = as_polars_series(
    c("000000", "ffff00", "0000ff")
  )$cast(pl$Binary)$bin$decode("hex")
)

df$with_columns(encoded = pl$col("code")$bin$encode("hex"))
#> shape: (3, 3)
#> ┌────────┬─────────────────┬─────────┐
#> │ name   ┆ code            ┆ encoded │
#> │ ---    ┆ ---             ┆ ---     │
#> │ str    ┆ binary          ┆ str     │
#> ╞════════╪═════════════════╪═════════╡
#> │ black  ┆ b"\x00\x00\x00" ┆ 000000  │
#> │ yellow ┆ b"\xff\xff\x00" ┆ ffff00  │
#> │ blue   ┆ b"\x00\x00\xff" ┆ 0000ff  │
#> └────────┴─────────────────┴─────────┘