Skip to content

Commit

Permalink
Merge pull request #459 from rstudio/bugfix/datetime-functions
Browse files Browse the repository at this point in the history
fix for conversion of date objects
  • Loading branch information
jjallaire authored Mar 14, 2019
2 parents 43f4be6 + c7da109 commit bfac80b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Install the development version with: `devtools::install_github("rstudio/reticul

- Fixed an issue where vectors of R Dates were not converted correctly. (#454)

- Fixed an issue where R Dates could not be passed to Python functions. (#458)

## reticulate 1.11.1 (CRAN)

- Fixed a failing virtual environment test on CRAN.
Expand Down
4 changes: 2 additions & 2 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ r_to_py.POSIXt <- function(x, convert = FALSE) {
if (py_module_available("numpy"))
return(np_array(as.numeric(x) * 1E9, dtype = "datetime64[ns]"))

datetime <- import("datetime", convert = convert)
datetime <- import("datetime", convert = FALSE)
datetime$datetime$fromtimestamp(as.double(x))
}

Expand All @@ -154,7 +154,7 @@ py_to_r.datetime.datetime <- function(x) {
#' @export
r_to_py.Date <- function(x, convert = FALSE) {

datetime <- import("datetime", convert = convert)
datetime <- import("datetime", convert = FALSE)
items <- lapply(x, function(item) {
iso <- strsplit(format(item), "-", fixed = TRUE)[[1]]
year <- as.integer(iso[[1]])
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-python-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ test_that("R times are converted to NumPy datetime64", {
)

})

test_that("R datetimes can be passed to Python functions", {
skip_if_no_python()
py_run_string("def identity(x): return x")
main <- import_main()
date <- Sys.Date()
expect_equal(date, main$identity(Sys.Date()))
})

0 comments on commit bfac80b

Please sign in to comment.