Skip to content

Commit d022e08

Browse files
authored
fix: Honor null_value precedence over deprecated null_values when exporting to CSV (#334)
1 parent 9ea51d4 commit d022e08

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

R/sink.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ sink_csv <- function(
276276
what = "sink_csv_polars(null_values)",
277277
details = "Use `null_value` instead."
278278
)
279-
if (!identical(null_value, "")) {
279+
if (missing(null_value)) {
280280
null_value <- null_values
281281
}
282282
}

R/write.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ write_csv_polars <- function(
8888
what = "write_csv_polars(null_values)",
8989
details = "Use `null_value` instead."
9090
)
91-
if (!identical(null_value, "")) {
91+
if (missing(null_value)) {
9292
null_value <- null_values
9393
}
9494
}

tests/testthat/test-sink.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ test_that("deprecated args in sink_csv()", {
2626
})
2727
})
2828

29+
test_that("sink_csv() resolves null_value and null_values correctly", {
30+
dat <- as_polars_lf(data.frame(x = c(1, NA), y = c(1, 2)))
31+
32+
dest_old <- tempfile(fileext = ".csv")
33+
expect_warning(
34+
sink_csv(dat, dest_old, null_values = "OLD"),
35+
"deprecated"
36+
)
37+
expect_identical(read.csv(dest_old)[2, "x"], "OLD")
38+
39+
dest_both <- tempfile(fileext = ".csv")
40+
expect_warning(
41+
sink_csv(dat, dest_both, null_value = "NEW", null_values = "OLD"),
42+
"deprecated"
43+
)
44+
expect_identical(read.csv(dest_both)[2, "x"], "NEW")
45+
})
46+
2947
test_that("basic behavior with parquet", {
3048
skip_if_not_installed("nanoparquet")
3149
dest <- tempfile(fileext = ".parquet")

tests/testthat/test-write.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ test_that("deprecated args in write_csv_polars()", {
4343
})
4444
})
4545

46+
test_that("write_csv_polars() resolves null_value and null_values correctly", {
47+
dat <- as_polars_df(data.frame(x = c(1, NA), y = c(1, 2)))
48+
49+
dest_old <- tempfile(fileext = ".csv")
50+
expect_warning(
51+
write_csv_polars(dat, dest_old, null_values = "OLD"),
52+
"deprecated"
53+
)
54+
expect_identical(read.csv(dest_old)[2, "x"], "OLD")
55+
56+
dest_both <- tempfile(fileext = ".csv")
57+
expect_warning(
58+
write_csv_polars(dat, dest_both, null_value = "NEW", null_values = "OLD"),
59+
"deprecated"
60+
)
61+
expect_identical(read.csv(dest_both)[2, "x"], "NEW")
62+
})
63+
4664
test_that("basic behavior with IPC", {
4765
skip_if_not_installed("arrow")
4866
dest <- tempfile(fileext = ".arrow")

0 commit comments

Comments
 (0)