diff --git a/NEWS.md b/NEWS.md index b0717d73c..dfdd8db75 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,7 @@ For now, `future = FALSE` can be replaced by `compat_level = FALSE` (#1183). - In `$scan_parquet()` and `$read_parquet()`, the default value of `hive_partitioning` is now `NULL` (#1189). +- In `$dt$epoch()`, the argument `tu` is renamed to `time_unit` (#1196). ### New features diff --git a/R/expr__datetime.R b/R/expr__datetime.R index 6d429c141..ddaba29a9 100644 --- a/R/expr__datetime.R +++ b/R/expr__datetime.R @@ -482,37 +482,30 @@ ExprDT_nanosecond = function() { } -# TODO: update the argument name and examples #' Epoch -#' @description +#' #' Get the time passed since the Unix EPOCH in the give time unit. #' -#' @param tu string option either 'ns', 'us', 'ms', 's' or 'd' -#' @details ns and perhaps us will exceed integerish limit if returning to -#' R as flaot64/double. -#' @return Expr of epoch as UInt32 -#' @keywords ExprDT -#' @aliases (Expr)$dt$epoch -#' @examples -#' as_polars_series(as.Date("2022-1-1"))$dt$epoch("ns") -#' as_polars_series(as.Date("2022-1-1"))$dt$epoch("ms") -#' as_polars_series(as.Date("2022-1-1"))$dt$epoch("s") -#' as_polars_series(as.Date("2022-1-1"))$dt$epoch("d") -ExprDT_epoch = function(tu = c("us", "ns", "ms", "s", "d")) { - tu = tu[1] +#' @param time_unit Time unit, one of `"ns"`, `"us"`, `"ms"`, `"s"` or `"d"`. +#' +#' @return Expr with datatype Int64 +#' @examples +#' df = pl$DataFrame(date = pl$date_range(as.Date("2001-1-1"), as.Date("2001-1-3"))) +#' +#' df$with_columns( +#' epoch_ns = pl$col("date")$dt$epoch(), +#' epoch_s = pl$col("date")$dt$epoch(time_unit = "s") +#' ) +ExprDT_epoch = function(time_unit = "us") { + time_unit = match.arg(time_unit, choices = c("us", "ns", "ms", "s", "d")) uw = \(res) unwrap(res, "in $dt$epoch:") - # experimental rust-like error handling on R side for the fun of it, sorry - # jokes aside here the use case is to tie various rust functions together - # and add context to the error messages - pcase( - !is_string(tu), Err("tu must be a string") |> uw(), - tu %in% c("ms", "us", "ns"), .pr$Expr$dt_timestamp(self, tu) |> uw(), - tu == "s", .pr$Expr$dt_epoch_seconds(self), - tu == "d", self$cast(pl$Date)$cast(pl$Int32), - or_else = Err( - paste("tu must be one of 'ns', 'us', 'ms', 's', 'd', got", str_string(tu)) - ) |> uw() + switch(time_unit, + "ms" = , + "us" = , + "ns" = .pr$Expr$dt_timestamp(self, time_unit) |> uw(), + "s" = .pr$Expr$dt_epoch_seconds(self), + "d" = self$cast(pl$Date)$cast(pl$Int32) ) } diff --git a/man/DataFrame_class.Rd b/man/DataFrame_class.Rd index 3bc8cc028..5b241516a 100644 --- a/man/DataFrame_class.Rd +++ b/man/DataFrame_class.Rd @@ -116,9 +116,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/DataFrame_to_data_frame.Rd b/man/DataFrame_to_data_frame.Rd index 1fc135a84..04401a24c 100644 --- a/man/DataFrame_to_data_frame.Rd +++ b/man/DataFrame_to_data_frame.Rd @@ -59,9 +59,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/DataFrame_to_list.Rd b/man/DataFrame_to_list.Rd index cf55bf3d2..766d7d1d0 100644 --- a/man/DataFrame_to_list.Rd +++ b/man/DataFrame_to_list.Rd @@ -69,9 +69,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/ExprDT_epoch.Rd b/man/ExprDT_epoch.Rd index 1fe21427c..2238014c1 100644 --- a/man/ExprDT_epoch.Rd +++ b/man/ExprDT_epoch.Rd @@ -2,28 +2,24 @@ % Please edit documentation in R/expr__datetime.R \name{ExprDT_epoch} \alias{ExprDT_epoch} -\alias{(Expr)$dt$epoch} \title{Epoch} \usage{ -ExprDT_epoch(tu = c("us", "ns", "ms", "s", "d")) +ExprDT_epoch(time_unit = "us") } \arguments{ -\item{tu}{string option either 'ns', 'us', 'ms', 's' or 'd'} +\item{time_unit}{Time unit, one of \code{"ns"}, \code{"us"}, \code{"ms"}, \code{"s"} or \code{"d"}.} } \value{ -Expr of epoch as UInt32 +Expr with datatype Int64 } \description{ Get the time passed since the Unix EPOCH in the give time unit. } -\details{ -ns and perhaps us will exceed integerish limit if returning to -R as flaot64/double. -} \examples{ -as_polars_series(as.Date("2022-1-1"))$dt$epoch("ns") -as_polars_series(as.Date("2022-1-1"))$dt$epoch("ms") -as_polars_series(as.Date("2022-1-1"))$dt$epoch("s") -as_polars_series(as.Date("2022-1-1"))$dt$epoch("d") +df = pl$DataFrame(date = pl$date_range(as.Date("2001-1-1"), as.Date("2001-1-3"))) + +df$with_columns( + epoch_ns = pl$col("date")$dt$epoch(), + epoch_s = pl$col("date")$dt$epoch(time_unit = "s") +) } -\keyword{ExprDT} diff --git a/man/LazyFrame_class.Rd b/man/LazyFrame_class.Rd index 3cf20de68..37b1144fb 100644 --- a/man/LazyFrame_class.Rd +++ b/man/LazyFrame_class.Rd @@ -86,9 +86,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/S3_as.data.frame.Rd b/man/S3_as.data.frame.Rd index 9223cc562..299859831 100644 --- a/man/S3_as.data.frame.Rd +++ b/man/S3_as.data.frame.Rd @@ -110,9 +110,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/S3_as.vector.Rd b/man/S3_as.vector.Rd index 850878e85..dea4207d7 100644 --- a/man/S3_as.vector.Rd +++ b/man/S3_as.vector.Rd @@ -46,9 +46,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/Series_class.Rd b/man/Series_class.Rd index 40185d9dd..bfb719b7d 100644 --- a/man/Series_class.Rd +++ b/man/Series_class.Rd @@ -143,9 +143,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/man/Series_to_r.Rd b/man/Series_to_r.Rd index 831bb49de..d77ee5fae 100644 --- a/man/Series_to_r.Rd +++ b/man/Series_to_r.Rd @@ -62,9 +62,7 @@ withr::with_timezone( \} ) #> -}\if{html}{\out{
}} -\if{html}{\out{
}}\preformatted{ withr::with_timezone( "America/New_York", \{ diff --git a/tests/testthat/test-datatype.R b/tests/testthat/test-datatype.R index c14065665..3066ce4f2 100644 --- a/tests/testthat/test-datatype.R +++ b/tests/testthat/test-datatype.R @@ -42,7 +42,7 @@ test_that("POSIXct data conversion", { pl$lit("2022-01-01")$str$strptime(pl$Datetime(), "%F")$to_r(), as.POSIXct("2022-01-01") ) - # TODO: infer timezone from string, change the arugment name from `tz` + # TODO: infer timezone from string expect_true( as_polars_series("2022-01-01 UTC")$str$strptime(pl$Datetime(time_zone = "UTC"), "%F %Z")$eq( as_polars_series(as.POSIXct("2022-01-01", tz = "UTC")) @@ -56,7 +56,7 @@ test_that("POSIXct data conversion", { pl$lit("2022-01-01")$str$strptime(pl$Datetime(), "%F")$to_r(), as.POSIXct("2022-01-01") ) - # TODO: infer timezone from string, change the arugment name from `tz` + # TODO: infer timezone from string expect_true( as_polars_series("2022-01-01 UTC")$str$strptime(pl$Datetime(time_zone = "UTC"), "%F %Z")$eq( as_polars_series(as.POSIXct("2022-01-01", tz = "UTC")) diff --git a/tests/testthat/test-expr_datetime.R b/tests/testthat/test-expr_datetime.R index d06cfc883..7d5df2711 100644 --- a/tests/testthat/test-expr_datetime.R +++ b/tests/testthat/test-expr_datetime.R @@ -460,11 +460,11 @@ test_that("dt$epoch", { expect_grepl_error( as_polars_series(as.Date("2022-1-1"))$dt$epoch("bob"), - "epoch: tu must be one of 'ns', 'us', 'ms', 's', 'd'" + "should be one of" ) expect_grepl_error( as_polars_series(as.Date("2022-1-1"))$dt$epoch(42), - "epoch: tu must be a string" + "must be NULL or a character vector" ) }) @@ -574,8 +574,8 @@ test_that("$convert_time_zone() works", { ) df_casts = df_time$with_columns( pl$col("date") - $dt$convert_time_zone("Europe/London") - $alias("London") + $dt$convert_time_zone("Europe/London") + $alias("London") ) orig_r = as.POSIXct( diff --git a/tests/testthat/test-lazy_functions.R b/tests/testthat/test-lazy_functions.R index cacd90345..cdf8e2a02 100644 --- a/tests/testthat/test-lazy_functions.R +++ b/tests/testthat/test-lazy_functions.R @@ -582,11 +582,12 @@ test_that("pl$int_range() works", { ) # custom data type - # TODO: this works with any dtype, how can I test this? - # expect_true(all.equal( - # as_polars_series(pl$int_range(0, 3, dtype = pl$Int16))$dtype, - # pl$Float32 - # )) + expect_true( + as_polars_series(pl$int_range(0, 3, dtype = pl$Int16))$dtype == pl$Int16 + ) + expect_false( + as_polars_series(pl$int_range(0, 3, dtype = pl$Int16))$dtype == pl$Int32 + ) expect_grepl_error( pl$int_range(0, 3, dtype = pl$String) |> as_polars_series(), @@ -624,13 +625,16 @@ test_that("pl$int_ranges() works", { list(int_range = list(c(1, 2), c(-1, 0, 1))) ) - # TODO: this works with any dtype, how can I test this? - # expect_true( - # all.equal( - # df$select(int_range = pl$int_ranges("start", "end", dtype = pl$Int16))$schema, - # list(int_range = pl$List(pl$Int16)) - # ) - # ) + expect_true( + df$select(int_range = pl$int_ranges("start", "end", dtype = pl$Int16))$ + schema$ + int_range == pl$List(pl$Int16) + ) + expect_false( + df$select(int_range = pl$int_ranges("start", "end", dtype = pl$Int16))$ + schema$ + int_range == pl$List(pl$String) + ) expect_grepl_error( df$select(int_range = pl$int_ranges("start", "end", dtype = pl$String)),