Skip to content

Commit

Permalink
Check numeric index for non-finite index values
Browse files Browse the repository at this point in the history
The check for finite index values was very early in the xts
constructor, which could cause problems if order.by wasn't strictly
a numeric vector (e.g. if it was POSIXlt--a list). This was to ensure
the index doesn't contain +/-Inf, NA, or NaN (see #173).

Move the check until after the index has been converted to
POSIXct/numeric, so we can be certain it is a numeric vector.

Fixes #194.
  • Loading branch information
joshuaulrich committed Jun 26, 2017
1 parent 1d0e4c1 commit ab858d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
7 changes: 4 additions & 3 deletions R/xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ 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)
if(!is.null(x)) {
Expand All @@ -88,6 +85,10 @@ function(x=NULL,
index <- as.numeric(as.POSIXct(strptime(as.character(order.by),"(%m/%d/%y %H:%M:%S)"))) #$format
else
index <- as.numeric(as.POSIXct(order.by))

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

x <- structure(.Data=x,
index=structure(index,tzone=tzone,tclass=orderBy),
class=c('xts','zoo'),
Expand Down
32 changes: 27 additions & 5 deletions inst/unitTests/runit.data.frame.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
data(sample_matrix)

sysTZ <- Sys.getenv('TZ')
Sys.setenv(TZ='GMT')
.setUp <- function() {
sysTZ <<- Sys.getenv("TZ")
Sys.setenv(TZ = "GMT")

sample.data.frame <- data.frame(sample_matrix)
sample.xts <- as.xts(sample.data.frame)
sample.data.frame <<- data.frame(sample_matrix)
sample.xts <<- as.xts(sample.data.frame)
}
.tearDown <- function() {
Sys.setenv(TZ = sysTZ)
}

test.convert_data.frame_to_xts <- function() {
checkIdentical(sample.xts,as.xts(sample.data.frame))
Expand Down Expand Up @@ -36,4 +41,21 @@ test.data.frame_reclass_subset_data.frame_j1 <- function() {
checkException(try.xts(sample.data.frame[,1]))
}

Sys.setenv(TZ=sysTZ)
# check for as.xts.data.frame when order.by is specified
test.convert_data.frame_to_xts_order.by_POSIXlt <- function() {
orderby = as.POSIXlt(rownames(sample.data.frame))
x <- as.xts(sample.data.frame, order.by = orderby)
y <- xts(coredata(sample.xts), as.POSIXlt(index(sample.xts)))
checkIdentical(y, x)
}
test.convert_data.frame_to_xts_order.by_POSIXct <- function() {
orderby = as.POSIXct(rownames(sample.data.frame))
x <- as.xts(sample.data.frame, order.by = orderby)
checkIdentical(sample.xts, x)
}
test.convert_data.frame_to_xts_order.by_Date <- function() {
orderby = as.Date(rownames(sample.data.frame))
x <- as.xts(sample.data.frame, order.by = orderby)
y <- xts(coredata(sample.xts), as.Date(index(sample.xts)))
checkIdentical(y, x)
}

0 comments on commit ab858d7

Please sign in to comment.