Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as.xts.data.table does not properly support all date based classes for index #2408

Closed
ethanbsmith opened this issue Oct 10, 2017 · 4 comments · Fixed by #2419
Closed

as.xts.data.table does not properly support all date based classes for index #2408

ethanbsmith opened this issue Oct 10, 2017 · 4 comments · Fixed by #2419
Milestone

Comments

@ethanbsmith
Copy link
Contributor

@ethanbsmith ethanbsmith commented Oct 10, 2017

xts objects support many date classes for the index, but the as.xts function only explicitly checks for small subset of classes.

fortunately, the xts::timeBased function will perform this check properly

i.e. replace
if (!any((index_class <- class(x[[1L]])) %in% c("POSIXct", "Date")))
with:
if(!xts::timeBased(x[[1L]]))

@ethanbsmith ethanbsmith changed the title as.xts.data.table doesnt support all date type index as.xts.data.table does not properly support all date based classes for index Oct 10, 2017
@ethanbsmith
Copy link
Contributor Author

@ethanbsmith ethanbsmith commented Oct 10, 2017

drop in replacement:

as.xts.data.table <- function(x, ...) {
    stopifnot(requireNamespace("xts"), !missing(x), is.data.table(x))
    if (!timeBased(x[[1L]])) stop("data.table must have a time based column in first position, use `setcolorder` function to change the order, or see ?timeBased for supported types")
    colsNumeric = sapply(x, is.numeric)[-1L]
    if (any(!colsNumeric)) warning(paste("Following columns are not numeric and will be omitted:", paste(names(colsNumeric)[!colsNumeric], collapse = ", ")))
    r = setDF(x[, .SD, .SDcols = names(colsNumeric)[colsNumeric]])
    xts::as.xts(r, order.by = x[[1L]])
}

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Oct 11, 2017

Nice catch! Feel free to file a PR, see Contribution Guidelines! happy to help facilitate

@ethanbsmith
Copy link
Contributor Author

@ethanbsmith ethanbsmith commented Oct 13, 2017

i have fixes and test in https://github.com/ebs238/data.table.
The fix works and looks clean to me, but I cant get the test to pass, and im not sure why.
This is my first attempt at a PR, so any suggestions would be appreciated

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Oct 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants