Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Dispatch to window.zoo() for yearmon and yearqtr
Before 0.11-0, window.zoo() was dispatched when window() was called
on a xts object because there was no window.xts() method. window.zoo()
supports additional types of values for the `start` argument, and
possibly other features. So 0.11-0 had breaking changes I did not
anticipate.

Thanks to GitHub user annaymj for the report.

Fixes #312.
  • Loading branch information
joshuaulrich committed Sep 15, 2020
1 parent 00c6122 commit eeae76f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions R/xts.methods.R
Expand Up @@ -374,6 +374,11 @@ window.xts <- function(x, index. = NULL, start = NULL, end = NULL, ...)
{
if(is.null(start) && is.null(end) && is.null(index.)) return(x)

# dispatch to window.zoo() for yearmon and yearqtr
if(tclass(x) %in% c("yearmon", "yearqtr")) {
return(NextMethod(.Generic))
}

firstlast <- window_idx(x, index., start, end) # firstlast may be NULL

.Call('_do_subset_xts',
Expand Down
21 changes: 21 additions & 0 deletions inst/unitTests/runit.subset.R
Expand Up @@ -283,3 +283,24 @@ test.duplicate_index_duplicate_i <- function() {

checkIdentical(x[index(x),], y)
}

test.window_yearmon_yearqtr_tclass_dispatches_to_zoo <- function() {
i1 <- seq(as.yearmon(2007), by = 1/12, length.out = 36)
x1 <- xts(1:36, i1)
i2 <- seq(as.yearqtr(2007), by = 1/4, length.out = 36)
x2 <- xts(1:36, i2)

r1 <- x1["2015"]
r2 <- x2["2015"]

# zoo supports numeric start for yearmon and yearqtr
w1 <- window(x1, start = 2015.01) # to window.zoo()
w2 <- window(x2, start = 2015.1) # to window.zoo()
checkEquals(r1, w1, "window, yearmon, numeric start")
checkEquals(r2, w2, "window, yearqtr, numeric start")

w1 <- window(x1, start = "2015-01-01") # to window.xts()
w2 <- window(x2, start = "2015Q1") # to window.zoo()
checkEquals(r1, w1, "window, yearmon, character start")
checkEquals(r2, w2, "window, yearqtr, character start")
}

0 comments on commit eeae76f

Please sign in to comment.