Skip to content

Rename the fields of the struct

Description

Rename the fields of the struct

Usage

<Expr>$struct$rename_fields(names)

Arguments

names New names, given in the same order as the struct’s fields.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  aaa = c(1, 2),
  bbb = c("ab", "cd"),
  ccc = c(TRUE, NA),
  ddd = list(1:2, 3)
)$select(struct_col = pl$struct("aaa", "bbb", "ccc", "ddd"))
df
#> shape: (2, 1)
#> ┌────────────────────────────┐
#> │ struct_col                 │
#> │ ---                        │
#> │ struct[4]                  │
#> ╞════════════════════════════╡
#> │ {1.0,"ab",true,[1.0, 2.0]} │
#> │ {2.0,"cd",null,[3.0]}      │
#> └────────────────────────────┘
df <- df$select(
  pl$col("struct_col")$struct$rename_fields(c("www", "xxx", "yyy", "zzz"))
)
df$select(pl$col("struct_col")$struct$field("*"))
#> shape: (2, 4)
#> ┌─────┬─────┬──────┬────────────┐
#> │ www ┆ xxx ┆ yyy  ┆ zzz        │
#> │ --- ┆ --- ┆ ---  ┆ ---        │
#> │ f64 ┆ str ┆ bool ┆ list[f64]  │
#> ╞═════╪═════╪══════╪════════════╡
#> │ 1.0 ┆ ab  ┆ true ┆ [1.0, 2.0] │
#> │ 2.0 ┆ cd  ┆ null ┆ [3.0]      │
#> └─────┴─────┴──────┴────────────┘
# Following a rename, the previous field names cannot be referenced:
tryCatch(
  {
    df$select(pl$col("struct_col")$struct$field("aaa"))
  },
  error = function(e) print(e)
)
#> <error/rlang_error>
#> Error in `df$select()`:
#> ! Evaluation failed in `$select()`.
#> Caused by error:
#> ! Evaluation failed in `$collect()`.
#> Caused by error:
#> ! field not found: aaa
#> 
#> Resolved plan until failure:
#> 
#>  ---> FAILED HERE RESOLVING 'sink' <---
#> DF ["struct_col"]; PROJECT */1 COLUMNS
#> ---
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   4. │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. └─df$select(pl$col("struct_col")$struct$field("aaa"))
#>   6.   ├─polars:::wrap(self$lazy()$select(...)$collect(`_eager` = TRUE)) at neo-r-polars/R/dataframe-frame.R:367:3
#>   7.   │ └─rlang::try_fetch(...) at neo-r-polars/R/utils-wrap.R:3:3
#>   8.   │   ├─base::tryCatch(...)
#>   9.   │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  10.   │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  11.   │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  12.   │   └─base::withCallingHandlers(...)
#>  13.   └─self$lazy()$select(...)$collect(`_eager` = TRUE) at neo-r-polars/R/utils-wrap.R:3:3
#>  14.     ├─polars:::wrap(...) at neo-r-polars/R/lazyframe-frame.R:284:3
#>  15.     │ └─rlang::try_fetch(...) at neo-r-polars/R/utils-wrap.R:3:3
#>  16.     │   ├─base::tryCatch(...)
#>  17.     │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  18.     │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  19.     │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  20.     │   └─base::withCallingHandlers(...)
#>  21.     └─ldf$collect(engine) at neo-r-polars/R/lazyframe-frame.R:331:5
#>  22.       └─polars:::.savvy_wrap_PlRDataFrame(...) at neo-r-polars/R/000-wrappers.R:3579:5