From 549a23ad804d1e4c744706c24937b46e9cbc7b47 Mon Sep 17 00:00:00 2001 From: Joshua Ulrich Date: Sun, 7 Apr 2024 10:25:27 -0500 Subject: [PATCH] Add warning if .xts() index is datetime The documentation for .xts() says the 'index' argument should be a numeric vector. Warn if it's a datetime because that has the potential to create malformed xts objects (e.g. the numeric index is POSIXct but its 'tzone' isn't UTC). --- R/xts.R | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/R/xts.R b/R/xts.R index 2fdfd552..30b8ca66 100644 --- a/R/xts.R +++ b/R/xts.R @@ -182,6 +182,24 @@ function(x=NULL, index, tclass=c("POSIXct","POSIXt"), if(check) { if( !isOrdered(index, increasing=TRUE, strictly=unique) ) stop('index is not in ',ifelse(unique, 'strictly', ''),' increasing order') + + if(timeBased(index)) { + msg <- paste0("'index' is not a plain numeric vector\n", + " this may create a malformed xts object and cause unexpected behaviors") + if(!inherits(index, tclass)) { + iclass <- class(index) + fmt <- "\n * index class (%s) does not match 'tclass' argument (%s)" + msg <- paste0(msg, + sprintf(fmt, + paste(iclass, collapse = ", "), + paste(tclass, collapse = ", "))) + } + if(isTRUE(tzone(index) != tzone)) { + fmt <- "\n * index timezone (%s) does not match 'tzone' argument (%s)" + msg <- paste0(msg, sprintf(fmt, dQuote(tzone(index)), dQuote(tzone))) + } + warning(msg) + } } index_out <- index