Skip to content

Commit

Permalink
Check for non-finite index values in constructors
Browse files Browse the repository at this point in the history
Several xts functions expect all index values to be finite. The most
important of them is merge.xts(). The constructors now error if any
potential index values are NA, NaN, or +/-Inf.

Fixes #173.
  • Loading branch information
joshuaulrich committed Jun 6, 2017
1 parent 5fd510f commit c24c682
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function(x=NULL,
order.by <- .POSIXct(unclass(order.by)*86400, tz=tzone)
}

if(any(!is.finite(order.by)))
stop("'order.by' cannot contain 'NA', 'NaN', or 'Inf'")

if(!isOrdered(order.by, strictly=!unique)) {
indx <- order(order.by)
Expand Down Expand Up @@ -112,6 +114,8 @@ function(x=NULL, index, tclass=c("POSIXct","POSIXt"),
index <- as.numeric(as.POSIXct(index))
if(!is.null(x) && NROW(x) != length(index))
stop("index length must match number of observations")
if(any(!is.finite(index)))
stop("'index' cannot contain 'NA', 'NaN', or 'Inf'")

if(!is.null(x)) {
if(!is.matrix(x))
Expand Down
45 changes: 45 additions & 0 deletions inst/unitTests/runit.xts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Tests for xts constructors
#

### NA in order.by {{{
# .xts()
test..xts_order.by_NA_integer <- function() {
checkException(.xts(1:3, c(1L, 2L, NA)))
checkException(.xts(1:3, c(NA, 2L, 3L)))
checkException(.xts(1:3, c(1L, NA, 3L)))
}
test..xts_order.by_NA_double <- function() {
checkException(.xts(1:3, c(1, 2, NA)))
checkException(.xts(1:3, c(NA, 2, 3)))
checkException(.xts(1:3, c(1, NA, 3)))
}
test..xts_order.by_NaN_double <- function() {
checkException(.xts(1:3, c(1, 2, NaN)))
checkException(.xts(1:3, c(NaN, 2, 3)))
checkException(.xts(1:3, c(1, NaN, 3)))
}
test..xts_order.by_Inf_double <- function() {
checkException(.xts(1:3, c(1, 2, Inf)))
checkException(.xts(1:3, c(-Inf, 2, 3)))
}
# xts()
test.xts_order.by_NA_integer <- function() {
checkException(xts(1:3, as.Date(c(1L, 2L, NA), origin = "1970-01-01")))
checkException(xts(1:3, as.Date(c(NA, 2L, 3L), origin = "1970-01-01")))
checkException(xts(1:3, as.Date(c(1L, NA, 3L), origin = "1970-01-01")))
}
test.xts_order.by_NA_double <- function() {
checkException(xts(1:3, .POSIXct(c(1, 2, NA))))
checkException(xts(1:3, .POSIXct(c(NA, 2, 3))))
checkException(xts(1:3, .POSIXct(c(1, NA, 3))))
}
test.xts_order.by_NaN_double <- function() {
checkException(xts(1:3, .POSIXct(c(1, 2, NaN))))
checkException(xts(1:3, .POSIXct(c(NaN, 2, 3))))
checkException(xts(1:3, .POSIXct(c(1, NaN, 3))))
}
test.xts_order.by_Inf_double <- function() {
checkException(xts(1:3, .POSIXct(c(1, 2, Inf))))
checkException(xts(1:3, .POSIXct(c(-Inf, 2, 3))))
}
### }}}

0 comments on commit c24c682

Please sign in to comment.