r - Why does one-row xts object not get a timezone? (why does it ignore/override the default argument) -
frustrated unit test complaining, narrowed down 1 xts
object has timezone set "utc", other has set "".
and i've narrowed down further seems creating xts object 1 row, compared 2+ rows:
> str(xts( c(1,2), as.posixct("2015-01-01 00:00:00")+0:1)) ‘xts’ object on 2015-01-01/2015-01-01 00:00:01 containing: data: num [1:2, 1] 1 2 indexed objects of class: [posixct,posixt] tz: utc xts attributes: null > str(xts( c(1), as.posixct("2015-01-01 00:00:01"))) ‘xts’ object on 2015-01-01 00:00:01/2015-01-01 00:00:01 containing: data: num [1, 1] 1 indexed objects of class: [posixct,posixt] tz: xts attributes: null
below xts
constructor. can see tzone
argument gets initialized sys.getenv("tz")
, evaluates "utc". i'm confused why tzone
ever end "", based on contents of x
.
function (x = null, order.by = index(x), frequency = null, unique = true, tzone = sys.getenv("tz"), ...) { if (is.null(x) && missing(order.by)) return(structure(.xts(, 0), index = integer())) if (!timebased(order.by)) stop("order.by requires appropriate time-based object") if (inherits(order.by, "dates")) tzone <- "" if (inherits(order.by, "date")) { if (!missing(tzone)) warning(paste(squote("tzone"), "setting ignored date indexes")) tzone <- "utc" } if (nrow(x) > 0 && nrow(x) != length(order.by)) stop("nrow(x) must match length(order.by)") orderby <- class(order.by) if (inherits(order.by, "date")) { order.by <- .posixct(unclass(order.by) * 86400, tz = tzone) } if (!isordered(order.by, strictly = !unique)) { indx <- order(order.by) if (!is.null(x)) { if (ncol(x) > 1 || is.matrix(x) || is.data.frame(x)) { x <- x[indx, , drop = false] } else x <- x[indx] } order.by <- order.by[indx] } if (!is.null(x) || length(x) != 0) { x <- as.matrix(x) } else x <- numeric(0) if (orderby == "timedate" && missing(tzone)) { tzone <- order.by@fincenter } else if (!is.null(attr(order.by, "tzone")) && missing(tzone)) tzone <- attr(order.by, "tzone") if (inherits(order.by, "dates")) index <- as.numeric(as.posixct(strptime(as.character(order.by), "(%m/%d/%y %h:%m:%s)"))) else index <- as.numeric(as.posixct(order.by)) x <- structure(.data = x, index = structure(index, tzone = tzone, tclass = orderby), class = c("xts", "zoo"), .indexclass = orderby, tclass = orderby, .indextz = tzone, tzone = tzone, ...) if (!is.null(attributes(x)$dimnames[[1]])) dimnames(x) <- dimnames(x) x }
this has posixct objects losing tzone
attribute when add integer sequence them. if create posixct vector using seq
, tzone
attribute retained. illustrate:
> attributes(as.posixct("2015-01-01")) $class [1] "posixct" "posixt" $tzone [1] "" > attributes(as.posixct("2015-01-01")+0) $class [1] "posixct" "posixt" > attributes(seq(as.posixct("2015-01-01"), by="sec", length.out=1)) $class [1] "posixct" "posixt" $tzone [1] ""
i need think bit more whether or not bug in constructor.
i don't think bug in xts constructor. issue constructor honors tzone
attribute if it's present, , sets sys.getenv("tz")
default if not. adding integer sequence posixct object removes tzone
attribute, that's why see behavior do.
if want specific timezone on index , you're creating via as.posixct
, need set tz
argument explicitly. example:
> str(xts(1, as.posixct("2015-01-01", tz=sys.getenv("tz")))) ‘xts’ object on 2015-01-01/2015-01-01 containing: data: num [1, 1] 1 indexed objects of class: [posixct,posixt] tz: utc xts attributes: null
Comments
Post a Comment