Skip to content

Rename the fields of the struct

Source code

Description

Rename the fields of the struct

Usage

<Expr>$struct$rename_fields(names)

Arguments

names Vector or list of strings given in the same order as the struct’s fields. Providing fewer names will drop the latter fields. If too many names are given, the extra names are ignored.

Value

Expr of datatype Struct

Examples

library("polars")

df = pl$DataFrame(
  aaa = 1:2,
  bbb = c("ab", "cd"),
  ccc = c(TRUE, NA),
  ddd = list(1:2, 3L)
)$select(
  pl$struct(pl$all())$alias("struct_col")
)$select(
  pl$col("struct_col")$struct$rename_fields(c("www", "xxx", "yyy", "zzz"))
)
df$unnest()
#> shape: (2, 4)
#> ┌─────┬─────┬──────┬───────────┐
#> │ www ┆ xxx ┆ yyy  ┆ zzz       │
#> │ --- ┆ --- ┆ ---  ┆ ---       │
#> │ i32 ┆ str ┆ bool ┆ list[i32] │
#> ╞═════╪═════╪══════╪═══════════╡
#> │ 1   ┆ ab  ┆ true ┆ [1, 2]    │
#> │ 2   ┆ cd  ┆ null ┆ [3]       │
#> └─────┴─────┴──────┴───────────┘