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

Accept vector of colnames in $over() #287

Merged
merged 3 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ for demonstration purposes (#240).
- `select()` now accepts lists of expressions. For example, `<DataFrame>$select(l_expr)`
works with `l_expr = list(pl$col("a"))` (#265).
- `<DataFrame>$glimpse()` is a fast `str()`-like view of a `DataFrame` (#277).
- `$over()` now accepts a vector of column names (#287).

# polars 0.6.1
## What's changed
Expand Down
12 changes: 12 additions & 0 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,9 @@ construct_ProtoExprArray = function(...) {

# if args not named load in Expr and string
if (is.null(arg_names)) {
if (length(args) == 1 && is.list(args)) {
args = unlist(args)
}
for (i in args) {
# if (is_string(i)) {
# pra$push_back_str(i)
Expand Down Expand Up @@ -2127,6 +2130,15 @@ Expr_last = "use_extendr_wrapper"
#' )$select(
#' pl$col("val")$count()$over("a", "b")
#' )
#'
#' over_vars = c("a", "b")
#' pl$DataFrame(
#' val = 1:5,
#' a = c("+", "+", "-", "-", "+"),
#' b = c("+", "-", "+", "-", "+")
#' )$select(
#' pl$col("val")$count()$over(over_vars)
#' )
Expr_over = function(...) {
# combine arguments in proto expression array
pra = construct_ProtoExprArray(...)
Expand Down
2 changes: 1 addition & 1 deletion man/DataFrame_glimpse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions man/Expr_over.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test-expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,36 @@ test_that("over", {
pl$col("val")$count()$over("a", pl$col("b"))
)

# with vector of column names
df2 = pl$DataFrame(list(
val = 1:5,
a = c("+", "+", "-", "-", "+"),
b = c("+", "-", "+", "-", "+")
))$select(
pl$col("val")$count()$over(c("a", "b"))
)

over_vars = c("a", "b")
df3 = pl$DataFrame(list(
val = 1:5,
a = c("+", "+", "-", "-", "+"),
b = c("+", "-", "+", "-", "+")
))$select(
pl$col("val")$count()$over(over_vars)
)

expect_equal(
as.numeric(df$get_column("val")$to_r()),
c(2, 1, 1, 1, 2)
)
expect_equal(
as.numeric(df2$get_column("val")$to_r()),
c(2, 1, 1, 1, 2)
)
expect_equal(
as.numeric(df3$get_column("val")$to_r()),
c(2, 1, 1, 1, 2)
)
})

test_that("col DataType + col(s) + col regex", {
Expand Down