You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/06/14 07:15:25 UTC

[GitHub] [arrow] nealrichardson commented on a change in pull request #10507: ARROW-13022: [R] bindings for lubridate's year, isoyear, quarter, month, day, wday, yday, isoweek, minute, and second functions

nealrichardson commented on a change in pull request #10507:
URL: https://github.com/apache/arrow/pull/10507#discussion_r650076060



##########
File path: r/R/expression.R
##########
@@ -28,8 +28,17 @@
   # stringr spellings of those
   "str_length" = "utf8_length",
   "str_to_lower" = "utf8_lower",
-  "str_to_upper" = "utf8_upper"
+  "str_to_upper" = "utf8_upper",
   # str_trim is defined in dplyr.R
+  "year" = "year",
+  "isoyear" = "iso_year",
+  "quarter" = "quarter",
+  "month" = "month",
+  "day" = "day",
+  "yday" = "day_of_year",
+  "isoweek" = "iso_week",
+  "minute" = "minute",
+  "second" = "second"

Review comment:
       IDK that we should truncate the data like this. A slight (technical) API difference is probably better than throwing away precision.

##########
File path: r/tests/testthat/test-dplyr-lubridate.R
##########
@@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+library(lubridate)
+library(dplyr)
+
+test_date <- ymd_hms("1987-10-09 23:00:00", tz = NULL)
+test_df <- tibble::tibble(date = test_date)
+
+test_that("extract datetime components from date", {
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = year(date)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = isoyear(date)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = quarter(date)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = month(date)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = wday(date)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_dplyr_equal(
+    input %>%
+      mutate(x = wday(date, week_start = 3)) %>%
+      collect(),
+    test_df
+  )
+  
+  expect_warning(
+    test_df %>%
+      Table$create() %>%
+      mutate(x = wday(date, label = TRUE)) %>%
+      collect(),
+    regexp = "Label argument not supported by Arrow; pulling data into R"
+  )
+  
+  expect_warning(
+    test_df %>%
+      Table$create() %>%
+      mutate(x = wday(date, locale = Sys.getlocale("LC_TIME"))) %>%
+      collect(),
+    regexp = 'Expression wday(date, locale = Sys.getlocale("LC_TIME")) not supported in Arrow; pulling data into R',
+    fixed = TRUE
+  )

Review comment:
       Yeah I think we can do this more unit-testy version where appropriate; we test elsewhere that the errors we throw in the nse_funcs get handled by filter/mutate correctly.

##########
File path: r/R/dplyr-functions.R
##########
@@ -442,3 +442,37 @@ nse_funcs$strptime <- function(x, format = "%Y-%m-%d %H:%M:%S", tz = NULL, unit
 
   Expression$create("strptime", x, options = list(format = format, unit = unit))
 }
+
+nse_funcs$wday <- function(x, label = FALSE, abbr = TRUE, week_start = getOption("lubridate.week.start", 7)) {
+  if (label) {
+    arrow_not_supported("Label argument")

Review comment:
       What is the label argument? Is it something we should support? I think it's good to document these unsupported exceptions in comments, to note either why Arrow should behave differently, or what Jira issue exists to remove the exception.

##########
File path: r/R/dplyr-functions.R
##########
@@ -442,3 +442,37 @@ nse_funcs$strptime <- function(x, format = "%Y-%m-%d %H:%M:%S", tz = NULL, unit
 
   Expression$create("strptime", x, options = list(format = format, unit = unit))
 }
+
+nse_funcs$wday <- function(x, label = FALSE, abbr = TRUE, week_start = getOption("lubridate.week.start", 7)) {
+  if (label) {
+    arrow_not_supported("Label argument")
+  }
+  offset <- get_date_offset(week_start)
+  Expression$create("add", Expression$create("day_of_week", x), Expression$scalar(offset))
+}
+
+#' Get date offset
+#' 
+#' Arrow's `day_of_week` kernel counts from 0 (Monday) to 6 (Sunday), whereas
+#' `lubridate::wday` counts from 1 to 7, and allows users to specify which day
+#' of the week is first (Sunday by default).  This function converts the returned

Review comment:
       Please leave a comment with that JIRA issue somewhere in this code that we expect to remove.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org