Skip to content

Convert DataFrame to a Series of type "struct"

Source code

Description

Convert DataFrame to a Series of type "struct"

Usage

<DataFrame>$to_struct(name = "")

Arguments

name Name given to the new Series

Value

A Series of type "struct"

Examples

library("polars")

# round-trip conversion from DataFrame with two columns
df = pl$DataFrame(a = 1:5, b = c("one", "two", "three", "four", "five"))
s = df$to_struct()
s
#> polars Series: shape: (5,)
#> Series: '' [struct[2]]
#> [
#>  {1,"one"}
#>  {2,"two"}
#>  {3,"three"}
#>  {4,"four"}
#>  {5,"five"}
#> ]
# convert to an R list
s$to_r()
#> $a
#> [1] 1 2 3 4 5
#> 
#> $b
#> [1] "one"   "two"   "three" "four"  "five" 
#> 
#> attr(,"is_struct")
#> [1] TRUE
# Convert back to a DataFrame
df_s = s$to_frame()
df_s
#> shape: (5, 1)
#> ┌─────────────┐
#> │             │
#> │ ---         │
#> │ struct[2]   │
#> ╞═════════════╡
#> │ {1,"one"}   │
#> │ {2,"two"}   │
#> │ {3,"three"} │
#> │ {4,"four"}  │
#> │ {5,"five"}  │
#> └─────────────┘