pola-rs / r-polars

Bring polars to R
https://pola-rs.github.io/r-polars/
Other
405 stars 35 forks source link

`map_elements` cause R session crach for `struct` object #993

Open Yunuuuu opened 2 months ago

Yunuuuu commented 2 months ago

map_elements crash for struct object, Here is the map_elements document for struct in Python: https://docs.pola.rs/user-guide/expressions/user-defined-functions/#combining-multiple-column-values

library(polars)
pl$DataFrame(mtcars)$
    select(pl$col("mpg", "cyl")$to_struct()$map_elements(\(x) x$mpg))

pl$DataFrame(mtcars)$
    select(pl$col("mpg", "cyl")$to_struct()$map_elements(\(x) x[["mpg"]]))

pl$DataFrame(mtcars)$
    select(
    pl$col("mpg", "cyl")$to_struct()$
        map_elements(\(x) x$struct$field("mpg"))
)
eitsupi commented 2 months ago

Struct type is not supported yet.

https://github.com/pola-rs/r-polars/blob/acc29eb62a44273164e2a1e66936e9f905aaa79b/src/rust/src/series.rs#L363-L390

PRs are welcome!

Yunuuuu commented 2 months ago

I'll try to implement it.

etiennebacher commented 2 months ago

Shorter reprex:

s = pl$Series(values = 1)$to_struct()
s$map_elements(\(x) x)

Error in .pr$Series$map_elements(self, fun, datatype, strict_return_type,  : 
  user function panicked: map_elements
etiennebacher commented 2 weeks ago

Turns out to_struct() shouldn't be available for all Expr, only for those in the list and arr subnamespaces: https://docs.pola.rs/py-polars/html/search.html?q=to_struct


Doesn't solve the issue though:

library(polars)

pl$DataFrame(mtcars)$
  select(pl$struct(c("mpg", "cyl"))$map_elements(\(x) x[["mpg"]]))
#>    1: user function raised an error: EvalError(lang!(function (s) {    s$map_elements(f, return_type, strict_return_type, allow_fail_eval)}, ExternalPtr.set_class(["RPolarsSeries"]))