Skip to content

Commit

Permalink
refactor: uncomment some tests, update $dt$epoch() (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Aug 22, 2024
1 parent fcdc85c commit 8088644
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 73 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 19 additions & 26 deletions R/expr__datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand Down
2 changes: 0 additions & 2 deletions man/DataFrame_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/DataFrame_to_data_frame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/DataFrame_to_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions man/ExprDT_epoch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/LazyFrame_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/S3_as.data.frame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/S3_as.vector.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/Series_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/Series_to_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-datatype.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-expr_datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})

Expand Down Expand Up @@ -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(
Expand Down
28 changes: 16 additions & 12 deletions tests/testthat/test-lazy_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 8088644

Please sign in to comment.