Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use $map_elements on struct columns? #1106

Closed
ToddMCr opened this issue May 25, 2024 · 2 comments
Closed

Is it possible to use $map_elements on struct columns? #1106

ToddMCr opened this issue May 25, 2024 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@ToddMCr
Copy link

ToddMCr commented May 25, 2024

I am trying to translate this python code

import polars as pl
import numpy as np
from numpy.random import normal

df = pl.DataFrame({
    "group": ["control", "treatment"],
    "alpha": [1,1], 
    "beta": [0, 1],
    "sigma": [1,1]
})

df = df.join(pl.DataFrame({"time": np.arange(0, 10+1)}),
             how="cross")

df = df.with_columns(
    pl.struct(["alpha", "beta", "sigma", "time"]).map_elements(
        function=lambda t: (
        pl.Series(name="y",
                  values=normal(t['alpha'] + t['beta']*t['time'], 
                                t['sigma'], 
                                3))
        ),
        return_dtype=pl.List(pl.Float64))
    .alias("y")
)

To R:

library(polars)

df = pl$DataFrame(group = c("control", "treatment"),
                  alpha = c(1, 1),
                  beta = c(0, 1),
                  sigma = c(1,1))

df = df$join(pl$DataFrame(time = 0:10), 
             how = "cross")

df$with_columns(
    pl$struct(c("alpha","beta","sigma","time"))$map_elements(\(t) {
        # generate data
        t
    })
)

But I am getting the following error even when I return the same element t:

[src/series.rs:444:17] x = Struct(
    [
        Field {
            name: "alpha",
            dtype: Float64,
        },
    ],
)
thread '<unnamed>' panicked at src/series.rs:445:17:
not yet implemented: this input type is not implemented
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread '<unnamed>' panicked at src/series.rs:91:1:
explicit panic
Error in .pr$Series$map_elements(self, fun, datatype, strict_return_type,  : 
  user function panicked: map_elements
Error: Execution halted with the following contexts
   0: In R: in $with_columns()
   0: During function call [df$with_columns(pl$struct("alpha")$map_elements(function(t) {
          t
      }))]
   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"])) 

I assume the not yet implemented: this input type is not implemented means that this isn't supported yet, but I just wanted to check if there was a work around or if iterating over struct columns is being worked on?

@eitsupi
Copy link
Collaborator

eitsupi commented May 26, 2024

Thanks for sharing your examples.

But I think this issue is a duplicate of #993

@eitsupi eitsupi closed this as not planned Won't fix, can't repro, duplicate, stale May 26, 2024
@eitsupi eitsupi added the duplicate This issue or pull request already exists label May 26, 2024
@ToddMCr
Copy link
Author

ToddMCr commented May 26, 2024

Ah sorry somehow I didn't see that issue, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants