Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-14808 [R] Implement bindings for lubridate::date()#12433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
da4158a1bce2a83ea6beb96c6b3f27cf3854ef422d35ddfec53b91ac87d809c3ea3048eea11c20ffe5555951160cefad691d9657262109569d487c04255041a3b0f11ad49d2c2c26ef79b6a9e24105b826e1be4e47f81190b8cfe32be4e31a280f4581e9addaf3d5cfd09bee85da34fb4f9d0c147587a941e604ffc7667c036e3495788fd5a549f4c1a9bbfdFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -76,6 +76,50 @@ register_bindings_type_cast <- function() { | ||
| register_binding("as.numeric", function(x) { | ||
| Expression$create("cast", x, options = cast_options(to_type = float64())) | ||
| }) | ||
| register_binding("as.Date", function(x, | ||
| format = NULL, | ||
| tryFormats = "%Y-%m-%d", | ||
| origin = "1970-01-01", | ||
| tz = "UTC") { | ||
| # the origin argument will be better supported once we implement temporal | ||
| # arithmetic (https://issues.apache.org/jira/browse/ARROW-14947) | ||
| # TODO revisit once the above has been sorted | ||
| if (call_binding("is.numeric", x) & origin != "1970-01-01") { | ||
| abort("`as.Date()` with an `origin` different than '1970-01-01' is not supported in Arrow") | ||
| } | ||
| # this could be improved with tryFormats once strptime returns NA and we | ||
| # can use coalesce - https://issues.apache.org/jira/browse/ARROW-15659 | ||
| # TODO revisit once https://issues.apache.org/jira/browse/ARROW-15659 is done | ||
| if (is.null(format) && length(tryFormats) > 1) { | ||
| abort("`as.Date()` with multiple `tryFormats` is not supported in Arrow") | ||
| } | ||
| if (call_binding("is.Date", x)) { | ||
| return(x) | ||
| # cast from POSIXct | ||
| } else if (call_binding("is.POSIXct", x)) { | ||
| # base::as.Date() first converts to the desired timezone and then extracts | ||
| # the date, which is why we need to go through timestamp() first | ||
| x <- build_expr("cast", x, options = cast_options(to_type = timestamp(timezone = tz))) | ||
dragosmg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # cast from character | ||
| } else if (call_binding("is.character", x)) { | ||
| format <- format %||% tryFormats[[1]] | ||
| # unit = 0L is the identifier for seconds in valid_time32_units | ||
| x <- build_expr("strptime", x, options = list(format = format, unit = 0L)) | ||
dragosmg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # cast from numeric | ||
| } else if (call_binding("is.numeric", x) & !call_binding("is.integer", x)) { | ||
| # Arrow does not support direct casting from double to date32() | ||
| # https://issues.apache.org/jira/browse/ARROW-15798 | ||
| # TODO revisit if arrow decides to support double -> date casting | ||
| abort("`as.Date()` with double/float is not supported in Arrow") | ||
| } | ||
| build_expr("cast", x, options = cast_options(to_type = date32())) | ||
| }) | ||
| register_binding("is", function(object, class2) { | ||
| if (is.string(class2)) { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -819,3 +819,88 @@ test_that("dst extracts daylight savings time correctly", { | ||
| test_df | ||
| ) | ||
| }) | ||
| test_that("date works in arrow", { | ||
| # https://issues.apache.org/jira/browse/ARROW-13168 | ||
| skip_on_os("windows") | ||
| # this date is specific since lubridate::date() is different from base::as.Date() | ||
| # since as.Date returns the UTC date and date() doesn't | ||
| test_df <- tibble( | ||
| posixct_date = as.POSIXct(c("2012-03-26 23:12:13", NA), tz = "America/New_York"), | ||
| integer_var = c(32L, NA)) | ||
| r_date_object <- lubridate::ymd_hms("2012-03-26 23:12:13") | ||
| # we can't (for now) use namespacing, so we need to make sure lubridate::date() | ||
| # and not base::date() is being used. This is due to the way testthat runs and | ||
| # normal use of arrow would not have to do this explicitly. | ||
| # TODO remove once https://issues.apache.org/jira/browse/ARROW-14575 is done | ||
| date <- lubridate::date | ||
dragosmg marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| compare_dplyr_binding( | ||
| .input %>% | ||
| mutate(a_date = date(posixct_date)) %>% | ||
| collect(), | ||
| test_df | ||
| ) | ||
| compare_dplyr_binding( | ||
| .input %>% | ||
| mutate(a_date_base = as.Date(posixct_date)) %>% | ||
| collect(), | ||
| test_df | ||
| ) | ||
| compare_dplyr_binding( | ||
| .input %>% | ||
| mutate(date_from_r_object = date(r_date_object)) %>% | ||
| collect(), | ||
| test_df | ||
| ) | ||
| compare_dplyr_binding( | ||
| .input %>% | ||
| mutate(as_date_from_r_object = as.Date(r_date_object)) %>% | ||
| collect(), | ||
| test_df | ||
| ) | ||
dragosmg marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| # date from integer supported in arrow (similar to base::as.Date()), but in | ||
| # Arrow it assumes a fixed origin "1970-01-01". However this is not supported | ||
| # by lubridate. lubridate::date(integer_var) errors without an `origin` | ||
| expect_equal( | ||
| test_df %>% | ||
| arrow_table() %>% | ||
| select(integer_var) %>% | ||
| mutate(date_int = date(integer_var)) %>% | ||
| collect(), | ||
| tibble(integer_var = c(32L, NA), | ||
| date_int = as.Date(c("1970-02-02", NA))) | ||
| ) | ||
| }) | ||
| test_that("date() errors with unsupported inputs", { | ||
| expect_error( | ||
| example_data %>% | ||
| arrow_table() %>% | ||
| mutate(date_char = date("2022-02-25 00:00:01")) %>% | ||
| collect(), | ||
| regexp = "Unsupported cast from string to date32 using function cast_date32" | ||
| ) | ||
| expect_error( | ||
| example_data %>% | ||
| arrow_table() %>% | ||
| mutate(date_bool = date(TRUE)) %>% | ||
| collect(), | ||
| regexp = "Unsupported cast from bool to date32 using function cast_date32" | ||
| ) | ||
| expect_error( | ||
| example_data %>% | ||
| arrow_table() %>% | ||
| mutate(date_double = date(34.56)) %>% | ||
| collect(), | ||
| regexp = "Unsupported cast from double to date32 using function cast_date32" | ||
| ) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.