Skip to content

Commit

Permalink
Implement $list$explode() (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Jun 10, 2024
1 parent 0dbb723 commit 0735544
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 1 addition & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
- for the Expr method only, the default value for `with_replacement` is now
`FALSE` (it was already the case for the DataFrame method).





### New features

- New method `$has_nulls()` (#1133).
- New method `$list$explode()` (#1139).

## Polars R Package 0.17.0

Expand Down
11 changes: 11 additions & 0 deletions R/expr__list.R
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,14 @@ ExprList_set_symmetric_difference = function(other) {
.pr$Expr$list_set_operation(self, other, "symmetric_difference") |>
unwrap("in $list$set_symmetric_difference():")
}

#' Returns a column with a separate row for every list element
#'
#' @inherit ExprList_set_union return
#'
#' @examples
#' df = pl$DataFrame(a = list(c(1, 2, 3), c(4, 5, 6)))
#' df$select(pl$col("a")$list$explode())
ExprList_explode = function() {
.pr$Expr$explode(self)
}
18 changes: 18 additions & 0 deletions man/ExprList_explode.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-expr_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,15 @@ test_that("$list$set_*() casts to common supertype", {
list(a = list(c("1.0", "2.0", "a", "b"), character(0)))
)
})

test_that("$list$explode() works", {
df = pl$DataFrame(a = list(c(1, 2, 3), c(4, 5, 6)))
expect_identical(
df$select(pl$col("a")$list$explode())$to_list(),
list(a = c(1, 2, 3, 4, 5, 6))
)
expect_error(
df$with_columns(pl$col("a")$list$explode()),
"lengths don't match"
)
})

0 comments on commit 0735544

Please sign in to comment.