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

Implement $cat$get_categories() #412

Merged
merged 2 commits into from
Oct 6, 2023
Merged

Implement $cat$get_categories() #412

merged 2 commits into from
Oct 6, 2023

Conversation

etiennebacher
Copy link
Collaborator

library(polars)

df = pl$DataFrame(
  cats = factor(c("z", "z", "k", "a", "b")),
  vals = factor(c(3, 1, 2, 2, 3))
)
df
#> shape: (5, 2)
#> ┌──────┬──────┐
#> │ cats ┆ vals │
#> │ ---  ┆ ---  │
#> │ cat  ┆ cat  │
#> ╞══════╪══════╡
#> │ z    ┆ 3    │
#> │ z    ┆ 1    │
#> │ k    ┆ 2    │
#> │ a    ┆ 2    │
#> │ b    ┆ 3    │
#> └──────┴──────┘

df$select(
  pl$col("cats")$cat$get_categories()
)
#> shape: (4, 1)
#> ┌──────┐
#> │ cats │
#> │ ---  │
#> │ str  │
#> ╞══════╡
#> │ z    │
#> │ k    │
#> │ a    │
#> │ b    │
#> └──────┘
df$select(
  pl$col("vals")$cat$get_categories()
)
#> shape: (3, 1)
#> ┌──────┐
#> │ vals │
#> │ ---  │
#> │ str  │
#> ╞══════╡
#> │ 3    │
#> │ 1    │
#> │ 2    │
#> └──────┘

@etiennebacher etiennebacher requested review from eitsupi and sorhawell and removed request for eitsupi October 5, 2023 20:31
Copy link
Collaborator

@sorhawell sorhawell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me :) thanks

@etiennebacher
Copy link
Collaborator Author

Thanks for the small fix. Quick question, how do you know that get_categories() is not fallible?

@etiennebacher etiennebacher merged commit ae0da68 into main Oct 6, 2023
11 checks passed
@etiennebacher etiennebacher deleted the get_categories branch October 6, 2023 21:13
@sorhawell
Copy link
Collaborator

sorhawell commented Oct 7, 2023

Thanks for the small fix. Quick question, how do you know that get_categories() is not fallible?

The rust-polars method return type Expr aka. pl::Expr
https://docs.rs/polars/latest/polars/prelude/struct.CategoricalNameSpace.html#method.get_categories

It would return PolarsResult if it was fallible. It could in theory also panic but those we just let crash and burn.

In generel any r-polars method which take no input and call a rust function/method which returns no result will be infallible. robj_to!() is fallible because user could give an erroneous input.

If binding together the output of two functions returning Expr but one is fallible the other one not. We can use Ok() to wrap it

let res_expr: Result<Expr> = if condition {
 fallible_f()
} else {
 Ok(infallible_f())
};

If only one infallible function, we don't need to wrap in an Ok, to just unwrap it again. It is not buggy, and everything would work as expected if we did.

@etiennebacher
Copy link
Collaborator Author

I see, thanks for the explanation, slowly getting more comfortable with the Rust side of polars :)

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

Successfully merging this pull request may close these issues.

None yet

2 participants