Skip to content

Commit

Permalink
Make it easier to create a DataFrame with a single list-column (#796)
Browse files Browse the repository at this point in the history
Co-authored-by: eitsupi <[email protected]>
  • Loading branch information
etiennebacher and eitsupi authored Feb 9, 2024
1 parent 268af93 commit 025e3be
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
is changed to `$versions$r_package` (#791).
- `$rust_polars`, which indicates the version of the dependent Rust Polars,
is changed to `$versions$rust_crate` (#791).
- New behavior when creating a `DataFrame` with a single list-variable.
`pl$DataFrame(x = list(1:2, 3:4))` used to create a `DataFrame` with two
columns named "new_column" and "new_column_1", which was unexpected. It now
produces a `DataFrame` with a single `list` variable. This also applies to
list-column created in `$with_columns()` and `$select()` (#794).

### Deprecations

Expand Down
5 changes: 3 additions & 2 deletions R/dotdotdot.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ unpack_list = function(..., .context = NULL, .call = sys.call(1L), skip_classes
l = list2(..., .context = .context, .call = .call)
if (
length(l) == 1L &&
is.list(l[[1L]]) &&
!(!is.null(skip_classes) && inherits(l[[1L]], skip_classes))
is.list(l[[1L]]) &&
!(!is.null(skip_classes) && inherits(l[[1L]], skip_classes)) &&
is.null(names(l))
) {
l[[1L]]
} else {
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ test_that("select with list of exprs", {
expect_equal(x6$columns, c("mpg", "hp"))
})

test_that("select: create a list variable", {
test = pl$DataFrame(x = 1:2)

# create one column
expect_identical(
test$select(y = list(1:2, 3:4))$to_list(),
list(y = list(1:2, 3:4))
)

# create several column
expect_identical(
test$select(y = list(1:2, 3:4), z = list(c("a", "b"), c("c", "d")))$to_list(),
list(y = list(1:2, 3:4), z = list(c("a", "b"), c("c", "d")))
)
})

test_that("map_batches unity", {
x = pl$
DataFrame(iris)$
Expand Down Expand Up @@ -375,6 +391,37 @@ test_that("get column", {

# TODO implement series cast and test Series_equal

test_that("with_columns: list or unlisted input", {
test = pl$DataFrame(mtcars)

# one element in $with_columns()
expect_identical(
test$with_columns(list(a = mtcars$drat))$to_data_frame(),
test$with_columns(a = mtcars$drat)$to_data_frame()
)

# several elements
expect_identical(
test$with_columns(list(a = 1, b = mtcars$drat))$to_data_frame(),
test$with_columns(a = 1, b = mtcars$drat)$to_data_frame()
)
})

test_that("with_columns: create a list variable", {
test = pl$DataFrame(x = 1:2)

# create one column
expect_identical(
test$with_columns(y = list(1:2, 3:4))$to_list(),
list(x = 1:2, y = list(1:2, 3:4))
)

# create several column
expect_identical(
test$with_columns(y = list(1:2, 3:4), z = list(c("a", "b"), c("c", "d")))$to_list(),
list(x = 1:2, y = list(1:2, 3:4), z = list(c("a", "b"), c("c", "d")))
)
})

test_that("with_columns lazy/eager", {
l = list(
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-expr_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test_that("list$sum max min mean", {
c(-Inf)
)

df = pl$DataFrame(list(x = ints))
df = pl$DataFrame(x = ints)
p_res = df$select(
pl$col("x")$list$sum()$alias("sum"),
pl$col("x")$list$max()$alias("max"),
Expand All @@ -63,7 +63,7 @@ test_that("list$sum max min mean", {
r_res
)

df = pl$DataFrame(list(x = floats))
df = pl$DataFrame(x = floats)
p_res = df$select(
pl$col("x")$list$sum()$alias("sum"),
pl$col("x")$list$max()$alias("max"),
Expand Down Expand Up @@ -404,7 +404,7 @@ test_that("concat", {

test_that("to_struct", {
l = list(integer(), 1:2, 1:3, 1:2)
df = pl$DataFrame(list(a = l))
df = pl$DataFrame(a = l)
act_1 = df$select(pl$col("a")$list$to_struct(
n_field_strategy = "first_non_null",
fields = \(idx) paste0("hello_you_", idx)
Expand Down Expand Up @@ -438,7 +438,7 @@ test_that("to_struct", {


test_that("eval", {
df = pl$DataFrame(a = list(a = c(1, 8, 3), b = c(4, 5, 2)))
df = pl$DataFrame(a = c(1, 8, 3), b = c(4, 5, 2))
l_act = df$with_columns(
pl$concat_list(list("a", "b"))$list$eval(pl$element()$rank())$alias("rank")
)$to_list()
Expand All @@ -454,7 +454,7 @@ test_that("eval", {

test_that("$list$all() works", {
df = pl$DataFrame(
list(a = list(c(TRUE, TRUE), c(FALSE, TRUE), c(FALSE, FALSE), NA, c()))
a = list(c(TRUE, TRUE), c(FALSE, TRUE), c(FALSE, FALSE), NA, c())
)
expect_identical(
df$select(all = pl$col("a")$list$all())$to_list(),
Expand All @@ -464,7 +464,7 @@ test_that("$list$all() works", {

test_that("$list$any() works", {
df = pl$DataFrame(
list(a = list(c(TRUE, TRUE), c(FALSE, TRUE), c(FALSE, FALSE), NA, c()))
a = list(c(TRUE, TRUE), c(FALSE, TRUE), c(FALSE, FALSE), NA, c())
)
expect_identical(
df$select(any = pl$col("a")$list$any())$to_list(),
Expand Down

0 comments on commit 025e3be

Please sign in to comment.