Skip to content

Commit 7b3b3d1

Browse files
Merge branch 'main' into polars-1.10.0
2 parents 08889b4 + 2a5cf25 commit 7b3b3d1

4 files changed

Lines changed: 59 additions & 33 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
* Fix `sample()` to make it work correctly (@Yousa-Mirage, #338).
2929

30+
* Fix `unite()` behavior when `na.rm = TRUE` (#344).
31+
3032
# tidypolars 0.17.0
3133

3234
`tidypolars` requires `polars` >= 1.9.0 and `dplyr` >= 1.2.0.

R/unite.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ unite.polars_data_frame <- function(
5050
col <- rlang::as_string(rlang::ensym(col))
5151

5252
if (isTRUE(na.rm)) {
53-
fill <- ""
53+
vars_to_concat <- vars
5454
} else {
55-
fill <- "NA"
55+
vars_to_concat <- list(pl$col(!!!vars)$fill_null("NA"))
5656
}
5757

5858
out <- data$with_columns(
59-
pl$concat_str(!!!vars, separator = sep, ignore_nulls = TRUE)$alias(col)
59+
pl$concat_str(
60+
!!!vars_to_concat,
61+
separator = sep,
62+
ignore_nulls = TRUE
63+
)$alias(col)
6064
)
6165

6266
out <- if (isTRUE(remove)) {

tests/testthat/test-unite-lazy.R

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test_that("basic behavior works", {
2121
)
2222
})
2323

24-
test_that("argument remove works", {
24+
test_that("argument 'remove' works", {
2525
test_df <- tibble(
2626
year = 2009:2011,
2727
month = 10:12,
@@ -52,6 +52,28 @@ test_that("argument remove works", {
5252
)
5353
})
5454

55+
test_that("argument 'na.rm' works", {
56+
test_df <- tibble(
57+
name = c(NA, "Jack", "Thomas"),
58+
middle = c("T.", NA, "F."),
59+
surname = c(NA, "Thompson", "Jones")
60+
)
61+
test_pl <- as_polars_lf(test_df)
62+
63+
expect_equal_lazy(
64+
unite(test_pl, col = "out", name, middle, surname, sep = "-"),
65+
unite(test_df, col = "out", name, middle, surname, sep = "-")
66+
)
67+
expect_equal_lazy(
68+
unite(test_pl, col = "out", name, middle, surname, na.rm = FALSE),
69+
unite(test_df, col = "out", name, middle, surname, na.rm = FALSE)
70+
)
71+
expect_equal_lazy(
72+
unite(test_pl, col = "out", name, middle, surname, na.rm = TRUE),
73+
unite(test_df, col = "out", name, middle, surname, na.rm = TRUE)
74+
)
75+
})
76+
5577
test_that("tidy selection works", {
5678
test_df <- tibble(
5779
name = c("John", "Jack", "Thomas"),
@@ -61,20 +83,8 @@ test_that("tidy selection works", {
6183
test_pl <- as_polars_lf(test_df)
6284

6385
expect_equal_lazy(
64-
unite(
65-
test_pl,
66-
col = "full_name",
67-
everything(),
68-
sep = " ",
69-
na.rm = TRUE
70-
),
71-
unite(
72-
test_df,
73-
col = "full_name",
74-
everything(),
75-
sep = " ",
76-
na.rm = TRUE
77-
)
86+
unite(test_pl, col = "full_name", everything(), sep = " "),
87+
unite(test_df, col = "full_name", everything(), sep = " ")
7888
)
7989
})
8090

tests/testthat/test-unite.R

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_that("basic behavior works", {
1717
)
1818
})
1919

20-
test_that("argument remove works", {
20+
test_that("argument 'remove' works", {
2121
test_df <- tibble(
2222
year = 2009:2011,
2323
month = 10:12,
@@ -48,6 +48,28 @@ test_that("argument remove works", {
4848
)
4949
})
5050

51+
test_that("argument 'na.rm' works", {
52+
test_df <- tibble(
53+
name = c(NA, "Jack", "Thomas"),
54+
middle = c("T.", NA, "F."),
55+
surname = c(NA, "Thompson", "Jones")
56+
)
57+
test_pl <- as_polars_df(test_df)
58+
59+
expect_equal(
60+
unite(test_pl, col = "out", name, middle, surname, sep = "-"),
61+
unite(test_df, col = "out", name, middle, surname, sep = "-")
62+
)
63+
expect_equal(
64+
unite(test_pl, col = "out", name, middle, surname, na.rm = FALSE),
65+
unite(test_df, col = "out", name, middle, surname, na.rm = FALSE)
66+
)
67+
expect_equal(
68+
unite(test_pl, col = "out", name, middle, surname, na.rm = TRUE),
69+
unite(test_df, col = "out", name, middle, surname, na.rm = TRUE)
70+
)
71+
})
72+
5173
test_that("tidy selection works", {
5274
test_df <- tibble(
5375
name = c("John", "Jack", "Thomas"),
@@ -57,20 +79,8 @@ test_that("tidy selection works", {
5779
test_pl <- as_polars_df(test_df)
5880

5981
expect_equal(
60-
unite(
61-
test_pl,
62-
col = "full_name",
63-
everything(),
64-
sep = " ",
65-
na.rm = TRUE
66-
),
67-
unite(
68-
test_df,
69-
col = "full_name",
70-
everything(),
71-
sep = " ",
72-
na.rm = TRUE
73-
)
82+
unite(test_pl, col = "full_name", everything(), sep = " "),
83+
unite(test_df, col = "full_name", everything(), sep = " ")
7484
)
7585
})
7686

0 commit comments

Comments
 (0)