Skip to content

Commit ef40791

Browse files
feat: Implement <group_by/lazy_group_by>$len() (#1638)
1 parent ce6ac67 commit ef40791

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `<lazyframe>$remove()` and `<dataframe>$remove()` as a complement to
99
`$filter()` (#1632).
1010
- New method `<expr>$is_close()` (#1637).
11+
- New methods `<group_by>$len()` and `<lazy_group_by>$len()` (#1638).
1112

1213
## polars 1.5.0
1314

R/dataframe-group_by-general.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,23 @@ groupby__n_unique <- function() {
207207
self$agg(pl$all()$n_unique()) |>
208208
wrap()
209209
}
210+
211+
#' @inherit lazygroupby__len title params
212+
#' @inherit as_polars_df return
213+
#' @examples
214+
#' df <- pl$DataFrame(
215+
#' a = c("Apple", "Apple", "Orange"),
216+
#' b = c(1, NA, 2)
217+
#' )
218+
#' df$group_by("a")$len()
219+
#'
220+
#' df$group_by("a")$len("n")
221+
groupby__len <- function(name = NULL) {
222+
wrap({
223+
len_expr <- pl$len()
224+
if (!is.null(name)) {
225+
len_expr <- len_expr$alias(name)
226+
}
227+
self$agg(len_expr)
228+
})
229+
}

R/lazyframe-group_by.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,26 @@ lazygroupby__n_unique <- function() {
207207
self$agg(pl$all()$n_unique()) |>
208208
wrap()
209209
}
210+
211+
#' Return the number of rows in each group
212+
#'
213+
#' @param name Assign a name to the resulting column. If `NULL`, defaults to
214+
#' `"len"`.
215+
#' @inherit as_polars_lf return
216+
#' @examples
217+
#' lf <- pl$LazyFrame(
218+
#' a = c("Apple", "Apple", "Orange"),
219+
#' b = c(1, NA, 2)
220+
#' )
221+
#' lf$group_by("a")$len()$collect()
222+
#'
223+
#' lf$group_by("a")$len("n")$collect()
224+
lazygroupby__len <- function(name = NULL) {
225+
wrap({
226+
len_expr <- pl$len()
227+
if (!is.null(name)) {
228+
len_expr <- len_expr$alias(name)
229+
}
230+
self$agg(len_expr)
231+
})
232+
}

altdoc/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ nav:
7474
- GroupBy:
7575
- agg: man/groupby__agg.md
7676
- head: man/groupby__head.md
77+
- len: man/groupby__len.md
7778
- mean: man/groupby__mean.md
7879
- median: man/groupby__median.md
7980
- min: man/groupby__min.md
@@ -155,6 +156,7 @@ nav:
155156
- GroupBy:
156157
- agg: man/lazygroupby__agg.md
157158
- head: man/lazygroupby__head.md
159+
- len: man/lazygroupby__len.md
158160
- mean: man/lazygroupby__mean.md
159161
- median: man/lazygroupby__median.md
160162
- min: man/lazygroupby__min.md

man/groupby__len.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/lazygroupby__len.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/lazyframe-frame.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,27 @@
628628
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
629629
[11] "carb"
630630

631+
# group_by() + len()
632+
633+
Code
634+
df$group_by("a", .maintain_order = TRUE)$len(1)
635+
Condition
636+
Error:
637+
! Evaluation failed in `$len()`.
638+
Caused by error in `len_expr$alias()`:
639+
! Evaluation failed in `$alias()`.
640+
Caused by error:
641+
! Argument `name` must be character, not double
642+
643+
---
644+
645+
Code
646+
df$group_by("a", .maintain_order = TRUE)$len(TRUE)
647+
Condition
648+
Error:
649+
! Evaluation failed in `$len()`.
650+
Caused by error in `len_expr$alias()`:
651+
! Evaluation failed in `$alias()`.
652+
Caused by error:
653+
! Argument `name` must be character, not logical
654+

tests/testthat/test-lazyframe-frame.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,3 +2616,29 @@ test_that("active bindings", {
26162616
expect_snapshot(as_polars_lf(mtcars)$width)
26172617
expect_snapshot(as_polars_lf(mtcars)$columns)
26182618
})
2619+
2620+
test_that("group_by() + len()", {
2621+
df <- pl$DataFrame(
2622+
a = c("Apple", "Apple", "Orange"),
2623+
b = c(1, NA, 2)
2624+
)
2625+
2626+
expect_query_equal(
2627+
.input$group_by("a", .maintain_order = TRUE)$len(),
2628+
df,
2629+
pl$DataFrame(a = c("Apple", "Orange"), len = c(2, 1))$cast(len = pl$UInt32)
2630+
)
2631+
expect_query_equal(
2632+
.input$group_by("a", .maintain_order = TRUE)$len("n"),
2633+
df,
2634+
pl$DataFrame(a = c("Apple", "Orange"), n = c(2, 1))$cast(n = pl$UInt32)
2635+
)
2636+
expect_snapshot(
2637+
df$group_by("a", .maintain_order = TRUE)$len(1),
2638+
error = TRUE
2639+
)
2640+
expect_snapshot(
2641+
df$group_by("a", .maintain_order = TRUE)$len(TRUE),
2642+
error = TRUE
2643+
)
2644+
})

0 commit comments

Comments
 (0)