Skip to content
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

Test write_parquet() temporal types w/ Python polars #128

Merged
merged 2 commits into from
Feb 17, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ jobs:
"~/.pip/pip.conf"
)
if (Sys.which("pip3") != "") {
system("pip3 install pyarrow pandas")
system("pip3 install pyarrow pandas polars")
} else {
system("pip install pyarrow pandas")
system("pip install pyarrow pandas polars")
}
shell: Rscript {0}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ jobs:
"~/.pip/pip.conf"
)
if (Sys.which("pip3") != "") {
system("pip3 install pyarrow pandas")
system("pip3 install pyarrow pandas polars")
} else {
system("pip install pyarrow pandas")
system("pip install pyarrow pandas polars")
}
shell: Rscript {0}

Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ skip_without_pyarrow <- function() {
}
}

skip_without_polars <- function() {
skip_on_cran()
if (tolower(Sys.getenv("_R_CHECK_FORCE_SUGGESTS_")) != "false") return()
pyscript <- r"[
import polars
]"
pytmp <- tempfile(fileext = ".py")
on.exit(unlink(pytmp), add = TRUE)
writeLines(pyscript, pytmp)
py <- if (Sys.which("python3") != "") "python3" else "python"
res <- tryCatch(
processx::run(py, pytmp, stderr = "2>&1"),
error = function(err) err
)
if (inherits(res, "error")) {
skip("missing polars in Python")
}
}

skip_without <- function(pkgs) {
if (any(c("arrow", "duckdb") %in% pkgs)) skip_on_cran()
if (tolower(Sys.getenv("_R_CHECK_FORCE_SUGGESTS_")) != "false") return()
Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-pypolars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
test_that("polars can read temporal types", {
skip_without_polars()

tmp <- tempfile(fileext = ".parquet")
on.exit(unlink(tmp), add = TRUE)

do <- function(df, path = tmp) {
write_parquet(df, path)
pyscript <- sprintf(r"[
import polars as pl
pl.read_parquet("%s")
]", normalizePath(path, winslash = "/"))
pytmp <- tempfile(fileext = ".py")
on.exit(unlink(pytmp), add = TRUE)
writeLines(pyscript, pytmp)
py <- if (Sys.which("python3") != "") "python3" else "python"
processx::run(py, pytmp, stderr = "2>&1")
}

# Date
df_date <- data.frame(x = Sys.Date())
expect_silent(do(df_date))

# hms, integer
# it is unclear if this ever comes up in practice
df_hmsi <- data.frame(
x = structure(0L, units = "secs", class = c("hms", "difftime"))
)
expect_silent(do(df_hmsi))

# hms, double
df_hmsd <- data.frame(x = hms::hms(0))
expect_silent(do(df_hmsd))

# difftime
df_difftime <- data.frame(x = as.difftime(1, units = "secs"))
expect_silent(do(df_difftime))

# POSIXct
df_posixct <- data.frame(x = Sys.time())
expect_silent(do(df_posixct))

# factor
df_factor <- data.frame(x = as.factor(c("a", "a")))
expect_silent(do(df_factor))
})
Loading