Skip to content

Commit

Permalink
Fix tclass when subset called on zero-length xts
Browse files Browse the repository at this point in the history
tclass() and tzone() were always the .xts() default values when [.xts
was called on a zero-length xts object. Thanks to Andre Mikulec for
the report and suggested patch!

Fixes #255.
  • Loading branch information
joshuaulrich committed Oct 9, 2022
1 parent 2eda3de commit 9438df4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion R/xts.methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ function(x, i, j, drop = FALSE, which.i=FALSE,...)
i <- seq_len(nr)

if(length(x)==0) {
x.tmp <- .xts(rep(NA,length(i)), .index(x)[i], dimnames=list(NULL, colnames(x)))
x.tmp <- .xts(rep(NA, length(i)), .index(x)[i], tclass(x), tzone(x),
dimnames = list(NULL, colnames(x)))
return(x.tmp)
} else {
if(USE_EXTRACT) {
Expand Down
18 changes: 13 additions & 5 deletions inst/unitTests/runit.subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ test.i_date_range_open_start <- function() {
test.empty_i_datetime <- function() {
d0 <- as.Date(integer())
zl <- xts(, d0)
empty <- .xts(logical(), d0, dim = 0:1, dimnames = list(NULL, NULL))
empty <- .xts(logical(), d0, "Date", "UTC", dim = 0:1, dimnames = list(NULL, NULL))

i <- Sys.Date()
checkIdentical(zl[i,], empty)
Expand All @@ -237,7 +237,7 @@ test.empty_i_datetime <- function() {
test.empty_i_zero <- function() {
d0 <- as.Date(integer())
zl <- xts(, d0)
empty <- .xts(logical(), d0, dim = 0:1, dimnames = list(NULL, NULL))
empty <- .xts(logical(), d0, "Date", "UTC", dim = 0:1, dimnames = list(NULL, NULL))

checkIdentical(zl[0,], empty)
checkIdentical(zl[0], empty)
Expand All @@ -246,7 +246,7 @@ test.empty_i_zero <- function() {
test.empty_i_negative <- function() {
d0 <- as.Date(integer())
zl <- xts(, d0)
empty <- .xts(logical(), d0, dim = 0:1, dimnames = list(NULL, NULL))
empty <- .xts(logical(), d0, "Date", "UTC", dim = 0:1, dimnames = list(NULL, NULL))

checkIdentical(zl[-1,], empty)
checkIdentical(zl[-1], empty)
Expand All @@ -255,7 +255,7 @@ test.empty_i_negative <- function() {
test.empty_i_NA <- function() {
d0 <- as.Date(integer())
zl <- xts(, d0)
empty <- .xts(logical(), d0, dim = 0:1, dimnames = list(NULL, NULL))
empty <- .xts(logical(), d0, "Date", "UTC", dim = 0:1, dimnames = list(NULL, NULL))

checkIdentical(zl[NA,], empty)
checkIdentical(zl[NA], empty)
Expand All @@ -264,7 +264,7 @@ test.empty_i_NA <- function() {
test.empty_i_NULL <- function() {
d0 <- as.Date(integer())
zl <- xts(, d0)
empty <- .xts(logical(), d0, dim = 0:1, dimnames = list(NULL, NULL))
empty <- .xts(logical(), d0, "Date", "UTC", dim = 0:1, dimnames = list(NULL, NULL))

checkIdentical(zl[NULL,], empty)
checkIdentical(zl[NULL], empty)
Expand Down Expand Up @@ -317,3 +317,11 @@ test.zero_width_subset_does_not_drop_user_attributes <- function(x) {
y <- x[,0]
checkEquals("hello", attr(y, "my_attr"))
}

test.zero_length_subset_xts_returns_same_tclass <- function() {
x <- .xts(matrix(1)[0,], integer(0), "Date")
checkTrue(tclass(x[0,]) == "Date")
x <- .xts(matrix(1)[0,], integer(0), "POSIXct", "America/Chicago")
checkTrue(tclass(x[0,]) == "POSIXct")
checkTrue(tzone(x[0,]) == "America/Chicago")
}

0 comments on commit 9438df4

Please sign in to comment.