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

feat: export $arr$to_list() #1018

Merged
merged 3 commits into from
Apr 9, 2024
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 @@ -109,6 +109,7 @@
- New functions `pl$int_range()` and `pl$int_ranges()` (#968).
- New string method `$str$extract_groups()` (#979).
- New string method `$str$find()` (#985).
- New array method `$arr$to_list()` (#1018).
- New argument `n` in `$str$replace()` (#987).
- Method `$over()` gains an argument `mapping_strategy` (#984, #988).
- New method `$item()` for `DataFrame` and `Series` (#992).
Expand Down
16 changes: 16 additions & 0 deletions R/expr__array.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ ExprArr_shift = function(periods = 1) {
unwrap("in $arr$shift():")
}


#' Convert an Array column into a List column with the same inner data type
#'
#' @return [Expr][Expr_class] of [data type List][DataType_List]
#' @examples
#' df = pl$DataFrame(
#' a = list(c(1, 2), c(3, 4)),
#' schema = list(a = pl$Array(pl$Int8, 2))
#' )
#'
#' df$with_columns(
#' list = pl$col("a")$arr$to_list()
#' )
ExprArr_to_list = function() .pr$Expr$arr_to_list(self)


#' Convert array to struct
#'
#' @inheritParams ExprList_to_struct
Expand Down
24 changes: 24 additions & 0 deletions man/ExprArr_to_list.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-expr_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ test_that("arr$shift", {
)
})

test_that("arr$to_list", {
df = pl$DataFrame(
strings = list(c("a", "b"), c("c", "d")),
schema = list(strings = pl$Array(pl$String, 2))
)

expect_true(
df$select(pl$col("strings")$arr$to_list())$equals(
pl$DataFrame(
strings = list(c("a", "b"), c("c", "d")),
schema = list(strings = pl$List(pl$String))
)
)
)
})

test_that("arr$to_struct", {
df = pl$DataFrame(
strings = list(c("a", "b"), c("c", "d")),
Expand Down
Loading