Skip to content

Commit

Permalink
Treat NA the same as NULL for start or end values
Browse files Browse the repository at this point in the history
NULL represents an undefined index value. NA represents an unknown or 
missing index value. xts does not allow NA as index values. Subsetting 
an xts or zoo object by NA returns a zero-length object. So a NA 
(unknown) index value is essentially the same as an undefined index 
value.

Closes #383. Fixes #345.
  • Loading branch information
harvey131 authored Feb 26, 2023
1 parent 2ec118b commit 8da2e87
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions R/xts.methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ window_idx <- function(x, index. = NULL, start = NULL, end = NULL)
# that is, index. must be time based,
window.xts <- function(x, index. = NULL, start = NULL, end = NULL, ...)
{
# scalar NA values are treated as NULL
if (isTRUE(is.na(start))) start <- NULL
if (isTRUE(is.na(end))) end <- NULL

if(is.null(start) && is.null(end) && is.null(index.)) return(x)

# dispatch to window.zoo() for yearmon and yearqtr
Expand Down
3 changes: 3 additions & 0 deletions inst/tinytest/test-subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ w2 <- window(x2, start = "2015Q1") # to window.zoo()
expect_equal(r1, w1, info = paste(info_msg, "window, yearmon, character start"))
expect_equal(r2, w2, info = paste(info_msg, "window, yearqtr, character start"))

w1 <- window(x1, start = "2015-01-01", end = NA) # to window.xts()
expect_equal(r1, w1, info = paste(info_msg, "window, yearmon, character start with end = NA"))

info_msg <- "test.zero_width_subset_does_not_drop_class"
target <- c("custom", "xts", "zoo")
x <- .xts(1:10, 1:10, class = target)
Expand Down
35 changes: 35 additions & 0 deletions inst/tinytest/test-xts.methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,41 @@ bin <- window(x)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- end = NULL, start = NULL"))

# Test just start, end = NA
start <- base + 13 * DAY
end <- base + 30*DAY
bin <- window(x, start = start, end = NA)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- just start, end = NA"))

# Test just start, end = NA, empty range
start <- base + 25 * DAY
end <- base + 30*DAY
bin <- window(x, start = start, end = NA)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- just start, end = NA, empty range"))

# Test just end, start = NA
end <- base + 13 * DAY
start <- base
bin <- window(x, start = NA, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- just end, start = NA"))

# Test just end, start = NA, empty range
end <- base
start <- base
bin <- window(x, start = NA, end = end)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- just end, start = NA, empty range"))

# Test end = NA, start = NA
start <- base
end <- base + 30*DAY
bin <- window(x, start = NA, end = NA)
reg <- window_dbg(x, start = start, end = end)
expect_identical(bin, reg, info = paste(info_msg, "- end = NA, start = NA"))

#######################################
# Test for index. parameter
start <- base
Expand Down

0 comments on commit 8da2e87

Please sign in to comment.