Skip to content

Commit fe990af

Browse files
jonkeanenealrichardson
authored andcommitted
ARROW-11136: [R] Bindings for is.nan
Closes apache#9167 from jonkeane/ARROW-11136-is.nan Authored-by: Jonathan Keane <jkeane@gmail.com> Signed-off-by: Neal Richardson <neal.p.richardson@gmail.com>
1 parent f291cd7 commit fe990af

6 files changed

Lines changed: 36 additions & 2 deletions

File tree

r/NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ S3method(is.na,ChunkedArray)
6464
S3method(is.na,Expression)
6565
S3method(is.na,Scalar)
6666
S3method(is.na,array_expression)
67+
S3method(is.nan,Array)
68+
S3method(is.nan,ChunkedArray)
6769
S3method(length,Array)
6870
S3method(length,ChunkedArray)
6971
S3method(length,Scalar)

r/R/array.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ length.Array <- function(x) x$length()
247247
#' @export
248248
is.na.Array <- function(x) call_function("is_null", x)
249249

250+
#' @export
251+
is.nan.Array <- function(x) call_function("is_nan", x)
252+
250253
#' @export
251254
as.vector.Array <- function(x, mode) x$as_vector()
252255

r/R/chunked-array.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ as.vector.ChunkedArray <- function(x, mode) x$as_vector()
130130
#' @export
131131
is.na.ChunkedArray <- function(x) call_function("is_null", x)
132132

133+
#' @export
134+
is.nan.ChunkedArray <- function(x) call_function("is_nan", x)
135+
133136
#' @export
134137
`[.ChunkedArray` <- filter_rows
135138

r/R/expression.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ cast_array_expression <- function(x, to_type, safe = TRUE, ...) {
111111

112112
.unary_function_map <- list(
113113
"!" = "invert",
114-
"is.na" = "is_null"
114+
"is.na" = "is_null",
115+
"is.nan" = "is_nan"
115116
)
116117

117118
.binary_function_map <- list(

r/tests/testthat/test-chunked-array.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ test_that("ChunkedArray handles NA", {
121121
expect_equal(as.vector(is.na(x)), c(is.na(data[[1]]), is.na(data[[2]]), is.na(data[[3]])))
122122
})
123123

124+
test_that("ChunkedArray handles NaN", {
125+
data <- list(as.numeric(1:10), c(NaN, 2:10), c(1:3, NaN, 5L))
126+
x <- chunked_array(!!!data)
127+
128+
expect_equal(x$type, float64())
129+
expect_equal(x$num_chunks, 3L)
130+
expect_equal(length(x), 25L)
131+
expect_equal(as.vector(x), c(1:10, c(NaN, 2:10), c(1:3, NaN, 5)))
132+
133+
chunks <- x$chunks
134+
expect_equal(as.vector(is.nan(chunks[[2]])), is.nan(data[[2]]))
135+
expect_equal(as.vector(is.nan(x)), c(is.nan(data[[1]]), is.nan(data[[2]]), is.nan(data[[3]])))
136+
})
137+
124138
test_that("ChunkedArray supports logical vectors (ARROW-3341)", {
125139
# with NA
126140
data <- purrr::rerun(3, sample(c(TRUE, FALSE, NA), 100, replace = TRUE))

r/tests/testthat/test-dataset.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ df1 <- tibble(
4444
second_date <- lubridate::ymd_hms("2017-03-09 07:01:02")
4545
df2 <- tibble(
4646
int = 101:110,
47-
dbl = as.numeric(51:60),
47+
dbl = c(as.numeric(51:59), NaN),
4848
lgl = rep(c(TRUE, FALSE, NA, TRUE, FALSE), 2),
4949
chr = letters[10:1],
5050
fct = factor(LETTERS[10:1]),
@@ -471,6 +471,17 @@ test_that("filter() with is.na()", {
471471
)
472472
})
473473

474+
test_that("filter() with is.nan()", {
475+
ds <- open_dataset(dataset_dir, partitioning = schema(part = uint8()))
476+
expect_equivalent(
477+
ds %>%
478+
select(part, dbl) %>%
479+
filter(!is.nan(dbl), part == 2) %>%
480+
collect(),
481+
tibble(part = 2L, dbl = df2$dbl[!is.nan(df2$dbl)])
482+
)
483+
})
484+
474485
test_that("filter() with %in%", {
475486
ds <- open_dataset(dataset_dir, partitioning = schema(part = uint8()))
476487
expect_equivalent(

0 commit comments

Comments
 (0)