Skip to content

Commit

Permalink
feat: export $arr$to_list() (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Apr 9, 2024
1 parent 3a08926 commit e3806c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
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

0 comments on commit e3806c9

Please sign in to comment.