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

Add as_polars_expr #835

Open
eitsupi opened this issue Feb 23, 2024 · 4 comments
Open

Add as_polars_expr #835

eitsupi opened this issue Feb 23, 2024 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@eitsupi
Copy link
Collaborator

eitsupi commented Feb 23, 2024

Related to #599, #570, #715

@eitsupi eitsupi added the enhancement New feature or request label Feb 23, 2024
@etiennebacher
Copy link
Collaborator

etiennebacher commented Feb 24, 2024

So basically you'd want to do the conversion to expressions in R rather than in Rust? Like the parse_* functions in Python? https://github.com/pola-rs/polars/blob/740e740d9ce3678ea061d5cb4c2bc94892838383/py-polars/polars/utils/_parse_expr_input.py

That would be a massive change internally. On the R side we also have wrap_e() that is used sometimes. Whether we choose to do the conversion to Expr on the R side or on the Rust side, I agree it would be good to harmonize this. This is also related to #422

@eitsupi
Copy link
Collaborator Author

eitsupi commented Feb 24, 2024

For example, the process of casting something like the Then class to Expr is currently handled by the match arm on the Rust side and cannot be added outside of this Rust crate.

_ if robj_inherits(&robj, ["RPolarsThen", "RPolarsChainedThen"]) => unpack_r_eval(
R!("polars:::result({{robj}}$otherwise(polars::pl$lit(NULL)))"),
)

I think this should be defined in a generic function on the R side. However, this would be a rather extensive change, so we did not make that change in #836.

@eitsupi
Copy link
Collaborator Author

eitsupi commented Sep 12, 2024

Seeing that scalar values were starting to get special treatment in upstream, I completely rewrote as_polars_expr() in the next branch (d83ceed).

Since there is no scalar class in R, it seems that <Expr>$first() must be called to convert the R object to a scalar value only if its length (strictly speaking, the length of the Series created from the R object) is 1.
c.f. pola-rs/polars#18686 (comment)

This change basically eliminates the need to describe the conversion from R to Polars in each of the two S3 methods, and if only as_polars_series() is defined, pl$lit() should also work properly.

library(neopolars)

lit0 <- pl$lit(hms::hms())
lit1 <- pl$lit(hms::as_hms("12:34:56"))
lit2 <- pl$lit(hms::as_hms(c("12:34:56", "23:45:56")))

lit0
#> Series[literal]

lit1
#> Series[literal].first()

lit2
#> Series[literal]

pl$select(lit0)
#> shape: (0, 1)
#> ┌─────────┐
#> │ literal │
#> │ ---     │
#> │ time    │
#> ╞═════════╡
#> └─────────┘

pl$select(lit1)
#> shape: (1, 1)
#> ┌──────────┐
#> │ literal  │
#> │ ---      │
#> │ time     │
#> ╞══════════╡
#> │ 12:34:56 │
#> └──────────┘

pl$select(lit2)
#> shape: (2, 1)
#> ┌──────────┐
#> │ literal  │
#> │ ---      │
#> │ time     │
#> ╞══════════╡
#> │ 12:34:56 │
#> │ 23:45:56 │
#> └──────────┘

Created on 2024-09-12 with reprex v2.1.1

@eitsupi eitsupi changed the title Add as_polars_lit and as_polars_expr Add as_polars_expr Sep 12, 2024
@eitsupi
Copy link
Collaborator Author

eitsupi commented Sep 22, 2024

Finally, it could be converted to Scalar as long as as_polars_series() is defined for the class.
dd88f54

library(neopolars)

lit0 <- pl$lit(hms::hms())
lit1 <- pl$lit(hms::as_hms("12:34:56"))
lit2 <- pl$lit(hms::as_hms(c("12:34:56", "23:45:56")))

lit0
#> Series[literal]

lit1
#> 12:34:56

lit2
#> Series[literal]

pl$select(lit0)
#> shape: (0, 1)
#> ┌─────────┐
#> │ literal │
#> │ ---     │
#> │ time    │
#> ╞═════════╡
#> └─────────┘

pl$select(lit1)
#> shape: (1, 1)
#> ┌──────────┐
#> │ literal  │
#> │ ---      │
#> │ time     │
#> ╞══════════╡
#> │ 12:34:56 │
#> └──────────┘

pl$select(lit2)
#> shape: (2, 1)
#> ┌──────────┐
#> │ literal  │
#> │ ---      │
#> │ time     │
#> ╞══════════╡
#> │ 12:34:56 │
#> │ 23:45:56 │
#> └──────────┘

Created on 2024-09-22 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants