This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new 7156b880 feat(r): Add support for creating timestamp and duration
types from numeric storage (#816)
7156b880 is described below
commit 7156b880de4777388cf6a71daef9e63c87bc4946
Author: Dewey Dunnington <[email protected]>
AuthorDate: Thu Oct 23 15:58:02 2025 -0500
feat(r): Add support for creating timestamp and duration types from numeric
storage (#816)
``` r
library(nanoarrow)
array <- as_nanoarrow_array(bit64::as.integer64(1:3), schema =
na_timestamp("s"))
convert_array(array)
#> [1] "1970-01-01 00:00:01 UTC" "1970-01-01 00:00:02 UTC"
#> [3] "1970-01-01 00:00:03 UTC"
array <- as_nanoarrow_array(bit64::as.integer64(1:3), schema =
na_duration("s"))
convert_array(array)
#> Time differences in secs
#> [1] 1 2 3
```
This mirrors what is possible in pyarrow:
```python
import numpy as np
import pyarrow as pa
storage = np.array([1, 2, 3], np.int64)
pa.array(storage, pa.timestamp("ns"))
#> <pyarrow.lib.TimestampArray object at 0x11a681720>
#> [
#> 1970-01-01 00:00:00.000000001,
#> 1970-01-01 00:00:00.000000002,
#> 1970-01-01 00:00:00.000000003
#> ]
pa.array(storage, pa.duration("ns"))
#> <pyarrow.lib.DurationArray object at 0x10f55b340>
#> [
#> 1,
#> 2,
#> 3
#> ]
```
Because the default integer type in Python is int64, creating these with
`[1, 2, 3]` also works; however, if these get constructed from an int32
we get a failure. In R our default integer type is int32, so I'm not
sure exactly what is best there. For now I'll just keep it to int64.
Closes #811.
---
r/DESCRIPTION | 2 +-
r/R/as-array.R | 4 +++-
r/man/na_vctrs.Rd | 2 +-
r/tests/testthat/test-as-array.R | 16 ++++++++++++++++
4 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/r/DESCRIPTION b/r/DESCRIPTION
index 0ef4cd27..8f94629c 100644
--- a/r/DESCRIPTION
+++ b/r/DESCRIPTION
@@ -19,7 +19,7 @@ Description: Provides an 'R' interface to the 'nanoarrow' 'C'
library and the
License: Apache License (>= 2)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
-RoxygenNote: 7.3.2
+RoxygenNote: 7.3.3
URL: https://arrow.apache.org/nanoarrow/latest/r/,
https://github.com/apache/arrow-nanoarrow
BugReports: https://github.com/apache/arrow-nanoarrow/issues
Suggests:
diff --git a/r/R/as-array.R b/r/R/as-array.R
index 3e350ef7..d3aa5ee9 100644
--- a/r/R/as-array.R
+++ b/r/R/as-array.R
@@ -90,7 +90,9 @@ as_nanoarrow_array.integer64 <- function(x, ..., schema =
NULL) {
switch(
parsed$type,
int64 = ,
- uint64 = {
+ uint64 = ,
+ timestamp = ,
+ duration = {
if (anyNA(x)) {
is_valid_lgl <- is.finite(x)
is_valid <- as_nanoarrow_array(is_valid_lgl, schema =
na_bool())$buffers[[2]]
diff --git a/r/man/na_vctrs.Rd b/r/man/na_vctrs.Rd
index c87dad6d..6b918ca8 100644
--- a/r/man/na_vctrs.Rd
+++ b/r/man/na_vctrs.Rd
@@ -24,7 +24,7 @@ through Arrow memory. The vctrs extension type uses
\code{\link[vctrs:vec_data]{
\code{\link[=as_nanoarrow_array]{as_nanoarrow_array()}} and
\code{\link[=convert_array]{convert_array()}} to ensure roundtrip fidelity.
}
\examples{
-\dontshow{if (requireNamespace("jsonlite", quietly = TRUE)) (if (getRversion()
>= "3.4") withAutoprint else force)(\{ # examplesIf}
+\dontshow{if (requireNamespace("jsonlite", quietly = TRUE)) withAutoprint(\{ #
examplesIf}
vctr <- as.POSIXlt("2000-01-02 03:45", tz = "UTC")
array <- as_nanoarrow_array(vctr, schema = na_vctrs(vctr))
infer_nanoarrow_ptype(array)
diff --git a/r/tests/testthat/test-as-array.R b/r/tests/testthat/test-as-array.R
index 79781721..6157bf55 100644
--- a/r/tests/testthat/test-as-array.R
+++ b/r/tests/testthat/test-as-array.R
@@ -330,6 +330,22 @@ test_that("as_nanoarrow_array() works for integer64() ->
na_int64()", {
expect_identical(convert_array(array, double()), as.double(c(1:10,
NA_real_)))
})
+test_that("as_nanoarrow_array() works for integer64() -> timezone/duration", {
+ skip_if_not_installed("bit64")
+
+ array <- as_nanoarrow_array(bit64::as.integer64(1:10), schema =
na_timestamp("s"))
+ expect_identical(
+ convert_array(array),
+ as.POSIXct(as.double(1:10), tz = "UTC", origin = "1970-01-01")
+ )
+
+ array <- as_nanoarrow_array(bit64::as.integer64(1:10), schema =
na_duration("s"))
+ expect_identical(
+ convert_array(array),
+ as.difftime(as.double(1:10), units = "secs")
+ )
+})
+
test_that("as_nanoarrow_array() works for double -> na_int8()", {
skip_if_not_installed("arrow")
casted <- as_nanoarrow_array(as.double(1:10), schema = na_int8())