diff --git a/R/translate.R b/R/translate.R index ed8f6a88..aff9069f 100644 --- a/R/translate.R +++ b/R/translate.R @@ -78,6 +78,17 @@ rel_find_packages <- function(name) { "minute" = "lubridate", "second" = "lubridate", "wday" = "lubridate", + "day" = "lubridate", + "month" = "lubridate", + "year" = "lubridate", + "ymd" = "lubridate", + "ydm" = "lubridate", + "mdy" = "lubridate", + "myd" = "lubridate", + "dmy" = "lubridate", + "dym" = "lubridate", + "quarter" = "lubridate", + "date" = "lubridate", "strftime" = "base", "substr" = "base", diff --git a/tests/testthat/test-as_duckplyr_df.R b/tests/testthat/test-as_duckplyr_df.R index cc22b22d..f73072ca 100644 --- a/tests/testthat/test-as_duckplyr_df.R +++ b/tests/testthat/test-as_duckplyr_df.R @@ -1647,6 +1647,67 @@ test_that("as_duckplyr_df_impl() and mutate(d = if_else(a > 1, \"ok\", NA))", { expect_identical(pre, post) }) +test_that("as_duckplyr_df_impl() and mutate(d = lubridate::day(d))", { + # Data + test_df <- data.frame(d = as.Date("2025-03-26")) + + # Run + pre <- test_df %>% as_duckplyr_df_impl() %>% mutate(d = lubridate::day(d)) + post <- test_df %>% mutate(d = lubridate::day(d)) %>% as_duckplyr_df_impl() + + # Compare + expect_identical(pre, post) +}) + +test_that("as_duckplyr_df_impl() and mutate(d = lubridate::month(d))", { + # Data + test_df <- data.frame(d = as.Date("2025-03-26")) + + # Run + pre <- test_df %>% as_duckplyr_df_impl() %>% mutate(d = lubridate::month(d)) + post <- test_df %>% mutate(d = lubridate::month(d)) %>% as_duckplyr_df_impl() + + # Compare + expect_identical(pre, post) +}) + + +test_that("as_duckplyr_df_impl() and mutate(d = lubridate::year(d))", { + # Data + test_df <- data.frame(d = as.Date("2025-03-26")) + + # Run + pre <- test_df %>% as_duckplyr_df_impl() %>% mutate(d = lubridate::year(d)) + post <- test_df %>% mutate(d = lubridate::year(d)) %>% as_duckplyr_df_impl() + + # Compare + expect_identical(pre, post) +}) + +test_that("as_duckplyr_df_impl() and mutate(d = lubridate::quarter(d))", { + # Data + test_df <- data.frame(d = as.Date("2025-03-26")) + + # Run + pre <- test_df %>% as_duckplyr_df_impl() %>% mutate(d = lubridate::quarter(d)) + post <- test_df %>% mutate(d = lubridate::quarter(d)) %>% as_duckplyr_df_impl() + + # Compare + expect_identical(pre, post) +}) + +test_that("as_duckplyr_df_impl() and mutate(d = lubridate::ymd(d))", { + # Data + test_df <- data.frame(d = "2025-03-26") + + # Run + pre <- test_df %>% as_duckplyr_df_impl() %>% mutate(d = lubridate::ymd(d)) + post <- test_df %>% mutate(d = lubridate::ymd(d)) %>% as_duckplyr_df_impl() + + # Compare + expect_identical(pre, post) +}) + test_that("as_duckplyr_df_impl() and n_groups()", { withr::local_envvar(DUCKPLYR_FORCE = "FALSE") diff --git a/tools/00-funs.R b/tools/00-funs.R index 96133789..d00137bd 100644 --- a/tools/00-funs.R +++ b/tools/00-funs.R @@ -853,6 +853,19 @@ test_extra_arg_map <- list( # NA in use 'd = if_else(a > 1, "ok", NA)', + # lubridate + "d = day(a)", + "d = month(a)", + "d = year(a)", + "d = ymd(a)", + "d = ydm(a)", + "d = mdy(a)", + "d = myd(a)", + "d = dmy(a)", + "d = dym(a)", + "d = quarter(a)", + "d = date(a)", + NULL ), nest_join = "join_by(a)", diff --git a/vignettes/limits.Rmd b/vignettes/limits.Rmd index f3108aa9..8b43c73b 100644 --- a/vignettes/limits.Rmd +++ b/vignettes/limits.Rmd @@ -186,7 +186,7 @@ duckplyr::duckdb_tibble(a = "abbc", .prudence = "stingy") |> ### Date manipulation -Implemented: `lubridate::hour()`, `lubridate::minute()`, `lubridate::second()`, `lubridate::wday()`. +Implemented: `lubridate::hour()`, `lubridate::minute()`, `lubridate::second()`, `lubridate::wday()`, `lubridate::month()`. ```{r} duckplyr::duckdb_tibble( @@ -194,10 +194,11 @@ duckplyr::duckdb_tibble( .prudence = "stingy" ) |> mutate( - hour = lubridate::hour(a), - minute = lubridate::minute(a), - second = lubridate::second(a), - wday = lubridate::wday(a) + hour = lubridate::hour(a), + minute = lubridate::minute(a), + second = lubridate::second(a), + wday = lubridate::wday(a), + month = lubridate::month(a) ) ```