Skip to content

Drop one or more fields from the struct

Source code

Description

Drop one or more fields from the struct

Usage

<Expr>$struct$drop(names, ..., strict = TRUE)

Arguments

names Character vector of the names of the fields to drop.
These dots are for future extensions and must be empty.
strict If TRUE (default), raise an error if any of the specified fields doesn’t exist in the struct.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  aaa = c(1, 2),
  bbb = c("ab", "cd"),
  ccc = c(TRUE, NA)
)$select(struct_col = pl$struct("aaa", "bbb", "ccc"))
df
#> shape: (2, 1)
#> ┌─────────────────┐
#> │ struct_col      │
#> │ ---             │
#> │ struct[3]       │
#> ╞═════════════════╡
#> │ {1.0,"ab",true} │
#> │ {2.0,"cd",null} │
#> └─────────────────┘
df$select(pl$col("struct_col")$struct$drop("aaa"))
#> shape: (2, 1)
#> ┌─────────────┐
#> │ struct_col  │
#> │ ---         │
#> │ struct[2]   │
#> ╞═════════════╡
#> │ {"ab",true} │
#> │ {"cd",null} │
#> └─────────────┘
# Dropping an unknown field is an error unless `strict = FALSE`
df$select(pl$col("struct_col")$struct$drop("zzz", strict = FALSE))
#> shape: (2, 1)
#> ┌─────────────────┐
#> │ struct_col      │
#> │ ---             │
#> │ struct[3]       │
#> ╞═════════════════╡
#> │ {1.0,"ab",true} │
#> │ {2.0,"cd",null} │
#> └─────────────────┘