代码之家  ›  专栏  ›  技术社区  ›  Darren Cook

为什么一行xts对象没有得到时区?(为什么忽略/覆盖默认参数)

xts r
  •  2
  • Darren Cook  · 技术社区  · 10 年前

    单元测试抱怨让我很沮丧,我把它缩小到一个 xts 对象的时区设置为“UTC”,另一个将其设置为“”。

    然后我进一步缩小了范围,似乎是用一行创建一个xts对象,而不是用两行以上的行:

    > 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
    

    下面是 文本 构造函数。你可以看到 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
    }
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Joshua Ulrich    10 年前

    这与POSIXct对象丢失 tzone 属性。如果使用 seq 这个 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] ""
    

    我需要多想一想,这是否是构造函数中的一个bug。


    我不认为这是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