由于单元测试抱怨而感到沮丧,我将其缩小为一个xts
对象,时区设置为" UTC",另一个将其设置为""。
然后我进一步缩小它,似乎是创建了一行xts对象,而不是2行:
> str(xts( c(1,2), as.POSIXct("2015-01-01 00:00:00")+0:1))
An ‘xts’ object on 2015-01-01/2015-01-01 00:00:01 containing:
Data: num [1:2, 1] 1 2
Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
xts Attributes:
NULL
> str(xts( c(1), as.POSIXct("2015-01-01 00:00:01")))
An ‘xts’ object on 2015-01-01 00:00:01/2015-01-01 00:00:01 containing:
Data: num [1, 1] 1
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
NULL
下面是xts
构造函数。您可以看到tzone
参数初始化为Sys.getenv("TZ")
,其评估为" UTC"。所以我很困惑为什么tzone
最终会以""为基础,基于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 an appropriate time-based object")
if (inherits(order.by, "dates"))
tzone <- ""
if (inherits(order.by, "Date")) {
if (!missing(tzone))
warning(paste(sQuote("tzone"), "setting ignored for 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
}
答案 0 :(得分:2)
这与POSIXct对象在向它们添加整数序列时丢失其tzone
属性有关。如果您使用seq
创建POSIXct向量,则会保留tzone
属性。举例说明:
> 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] ""
我需要更多地思考这是否是构造函数中的错误。
我不认为这是xts构造函数中的错误。问题是构造函数会在tzone
属性存在的情况下对其进行授予,如果不存在,则默认将其设置为Sys.getenv("TZ")
。将整数序列添加到POSIXct对象会删除tzone
属性,这就是您看到行为的原因。
如果您想在索引上设置特定时区,并且需要通过as.POSIXct
创建该时区,则需要明确设置tz
参数。例如:
> str(xts(1, as.POSIXct("2015-01-01", tz=Sys.getenv("TZ"))))
An ‘xts’ object on 2015-01-01/2015-01-01 containing:
Data: num [1, 1] 1
Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
xts Attributes:
NULL