Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions r/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* `read_csv_arrow()`'s readr-style type `T` is now mapped to `timestamp(unit = "ns")` instead of `timestamp(unit = "s")`.
* `lubridate`:
* component extraction functions: `tz()` (timezone), `semester()` (semester), `dst()` (daylight savings time indicator), `date()` (extract date), `epiyear()` (epiyear), improvements to `month()`, which now works with integer inputs.
* `make_date()` and `make_datetime()` to create `Date` objects from numeric parts.
* date-time functionality:
* `as.Date()` to convert to date

Expand Down
21 changes: 21 additions & 0 deletions r/R/dplyr-funcs-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@ register_bindings_datetime <- function() {
register_binding("date", function(x) {
build_expr("cast", x, options = list(to_type = date32()))
})
register_binding("make_date", function(year = 1970L, month = 1L, day = 1L) {
x <- call_binding("paste", year, month, day, sep = "-")
x <- call_binding("strptime", x, format = "%Y-%m-%d", unit = "s")
build_expr("cast", x, options = cast_options(to_type = date32()))
})
register_binding("make_datetime", function(year = 1970L,
month = 1L,
day = 1L,
hour = 0L,
min = 0L,
sec = 0,
tz = NULL) {
x <- call_binding("paste", year, month, day, hour, min, sec, sep = "-")
# ParseTimestampStrptime currently ignores the timezone information (ARROW-12820).
# Stop if tz is provided.
if (is.character(tz)) {
arrow_not_supported("Time zone argument")
}
Comment thread
dragosmg marked this conversation as resolved.
Outdated

build_expr("strptime", x, options = list(format = "%Y-%m-%d-%H-%M-%S", unit = 0L))
})
Comment thread
dragosmg marked this conversation as resolved.
Outdated
}

binding_format_datetime <- function(x, format = "", tz = "", usetz = FALSE) {
Expand Down
47 changes: 47 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,50 @@ test_that("date() errors with unsupported inputs", {
regexp = "Unsupported cast from double to date32 using function cast_date32"
)
})

test_that("make_date & make_datetime", {
set.seed(12345)
test_df <- tibble(
year = sample(1969:2069, 12),
month = 1:12,
day = sample(1:28, 12, replace = TRUE),
hour = sample(0:23, 12, replace = TRUE),
min = sample(0:59, 12),
sec = sample(0:59, 12)
)
Comment thread
dragosmg marked this conversation as resolved.
Outdated

compare_dplyr_binding(
.input %>%
mutate(composed_date = make_date(year, month, day)) %>%
collect(),
test_df
)

compare_dplyr_binding(
.input %>%
mutate(composed_date_r_obj = make_date(1999, 12, 31)) %>%
collect(),
test_df
)

compare_dplyr_binding(
.input %>%
mutate(composed_datetime = make_datetime(year, month, day, hour, min, sec)) %>%
collect(),
test_df,
# the make_datetime binding uses strptime which does not support tz, hence
# a mismatch in tzone attribute (ARROW-12820)
ignore_attr = TRUE
)

compare_dplyr_binding(
.input %>%
mutate(
composed_datetime_r_obj = make_datetime(1999, 12, 31, 14, 15, 16)) %>%
collect(),
test_df,
# the make_datetime binding uses strptime which does not support tz, hence
# a mismatch in tzone attribute (ARROW-12820)
ignore_attr = TRUE
)
})