joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

`endpoints()` should return consistently an error when not k > 0 #301

Closed Eluvias closed 3 years ago

Eluvias commented 5 years ago

endpoints() throws an error when not k > 0 for all cases of on except of the following: years, quarters and months. This is because the error is raised on the cases where the C implementation is used which checks for the values of k.

The case of on = months is an exception as here the C implementation is used but the k value is fixed at 1L.

months = {
        ixmon <- posixltindex$year * 100L + 190000L + posixltindex$mon
        ep <- .Call("endpoints", ixmon, 1L, 1L, addlast, 
            PACKAGE = "xts")
        if (k > 1) ep[seq(1, length(ep), k)] else ep

The cases of on = quarters or on = years are using the R implementation and they don't check for k > 0.

Examples

library(xts)
xx <- as.xts(1:30,
              seq.Date(as.Date(Sys.Date()),
                       by = "day",
                       length.out = 30))

Error is raised when not k > 0

 endpoints(xx, on = "ms", k = -1)
# Error in endpoints(xx, on = "ms", k = -1) : 'k' must be > 0

 endpoints(xx, on = "minutes", k = -1)
# Error in endpoints(xx, on = "minutes", k = -1) : 'k' must be > 0

 endpoints(xx, on = "days", k = -1)
# Error in endpoints(xx, on = "days", k = -1) : 'k' must be > 0

 endpoints(xx, on = "week", k = -1)
# Error in endpoints(xx, on = "week", k = -1) : 'k' must be > 0

Error is not raised when not k > 0

 endpoints(xx, on = "months", k = -1)
> [1]  0 29 30

 endpoints(xx, on = "quarters", k = -1)
> [1]  0 29 30

 endpoints(xx, on = "years", k = -1)
> [1]  0 30
joshuaulrich commented 3 years ago

Thanks for the report!

Eluvias commented 3 years ago

Thanks a lot.