Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# tidypolars (development)

`tidypolars` requires `polars` >= 1.10.0.

## New features

* Added support for the following functions:

- `anyNA()` (#330)
- `cummax()` (#323)
- `trunc()` (#343)

* Added `decreasing` and `na.last` arguments support to `sort()` (@Yousa-Mirage, #328).

Expand Down
5 changes: 5 additions & 0 deletions R/funs-default.R
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ pl_tanh <- function(x) {
x$tanh()
}

pl_trunc <- function(x, ...) {
check_empty_dots(...)
x$truncate(decimals = 0)
}

pl_unique <- function(x, ...) {
check_empty_dots(...)
x$unique()
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/_snaps/funs_default-lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@
Caused by error:
! lengths don't match: unable to add a column of length 4 to a DataFrame of height 5

# trunc() works

Code
current$collect()
Condition
Error in `current$collect()`:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

# trunc() in tidypolars doesn't support Date/datetime

Code
current$collect()
Condition
Warning:
tidypolars doesn't know how to use some arguments of `trunc()`.
i The following argument(s) will be ignored: "units".
Error in `current$collect()`:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

---

Code
current$collect()
Condition
Warning:
tidypolars doesn't know how to use some arguments of `trunc()`.
i The following argument(s) will be ignored: "units".
Error in `current$collect()`:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

# sample() validates size

Code
Expand Down
42 changes: 42 additions & 0 deletions tests/testthat/_snaps/funs_default.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@
Caused by error:
! lengths don't match: unable to add a column of length 4 to a DataFrame of height 5

# trunc() works

Code
mutate(test_pl, foo = trunc("a"))
Condition
Error in `.data$with_columns()`:
! Evaluation failed in `$with_columns()`.
Caused by error:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

# trunc() in tidypolars doesn't support Date/datetime

Code
mutate(test_pl, x = trunc(date, units = "secs"))
Condition
Warning:
tidypolars doesn't know how to use some arguments of `trunc()`.
i The following argument(s) will be ignored: "units".
Error in `.data$with_columns()`:
! Evaluation failed in `$with_columns()`.
Caused by error:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

---

Code
mutate(test_pl, x = trunc(datetime, units = "secs"))
Condition
Warning:
tidypolars doesn't know how to use some arguments of `trunc()`.
i The following argument(s) will be ignored: "units".
Error in `.data$with_columns()`:
! Evaluation failed in `$with_columns()`.
Caused by error:
! Evaluation failed in `$collect()`.
Caused by error:
! truncation ('to_zero') can only be used on numeric types

# sample() validates size

Code
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-funs_default-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,39 @@ test_that("round() works", {
)
})

test_that("trunc() works", {
test_df <- tibble(x = c(0.33, 0.5212, NA))
test_pl <- as_polars_lf(test_df)

expect_equal_lazy(
test_pl |> mutate(foo = trunc(x)),
test_df |> mutate(foo = trunc(x))
)
expect_both_error(
test_pl |> mutate(foo = trunc("a")),
test_df |> mutate(foo = trunc("a"))
)
expect_snapshot_lazy(
test_pl |> mutate(foo = trunc("a")),
error = TRUE
)
})

test_that("trunc() in tidypolars doesn't support Date/datetime", {
test_pl <- pl$LazyFrame(
date = as.Date("2020-01-01"),
datetime = as.POSIXct("2020-01-01")
)
expect_snapshot_lazy(
test_pl |> mutate(x = trunc(date, units = "secs")),
error = TRUE
)
expect_snapshot_lazy(
test_pl |> mutate(x = trunc(datetime, units = "secs")),
error = TRUE
)
})

test_that("sample() works with default size and n() size", {
test_df <- tibble(x = 1:5)
test_pl <- as_polars_lf(test_df)
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-funs_default.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ test_that("round() works", {
)
})

test_that("trunc() works", {
test_df <- tibble(x = c(0.33, 0.5212, NA))
test_pl <- as_polars_df(test_df)

expect_equal(
test_pl |> mutate(foo = trunc(x)),
test_df |> mutate(foo = trunc(x))
)
expect_both_error(
test_pl |> mutate(foo = trunc("a")),
test_df |> mutate(foo = trunc("a"))
)
expect_snapshot(
test_pl |> mutate(foo = trunc("a")),
error = TRUE
)
})

test_that("trunc() in tidypolars doesn't support Date/datetime", {
test_pl <- pl$DataFrame(
date = as.Date("2020-01-01"),
datetime = as.POSIXct("2020-01-01")
)
expect_snapshot(
test_pl |> mutate(x = trunc(date, units = "secs")),
error = TRUE
)
expect_snapshot(
test_pl |> mutate(x = trunc(datetime, units = "secs")),
error = TRUE
)
})

test_that("sample() works with default size and n() size", {
test_df <- tibble(x = 1:5)
test_pl <- as_polars_df(test_df)
Expand Down
3 changes: 3 additions & 0 deletions vignettes/supported-functions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ out <- tribble(
"`base`", "`tanh`",
"`base`", "`tolower`",
"`base`", "`toupper`",
"`base`", "`trunc`",
"`base`", "`unique`",
"`base`", "`which.min`",
"`base`", "`which.max`",
Expand Down Expand Up @@ -190,6 +191,8 @@ out <- tribble(
"In `tidypolars`, `na.last` must be explicitly supplied as `TRUE` or `FALSE`.",
Package == "`base`" & Function == "`sample`" ~
"`set.seed()` is not supported. Randomness is handled by Polars and does not use R's RNG state.",
Package == "`base`" & Function == "`trunc`" ~
"This only works with numeric data in `tidypolars`, while it works with other types (such as dates) in base R.",
Package == "`lubridate`" & Function %in% c("`rollbackward`", "`rollback`", "`rollforward`") ~
"While time zone handling should mimick the behaviour of `lubridate` in most cases, it is possible that Polars errors if rolling back/forward leads to am ambiguous datetime. It is also possible to have some differences in hours/minutes/seconds when converting between Polars and R.",
Package == "`lubridate`" & Function == "`wday`" ~
Expand Down
Loading