From ccb603dc839f2dc3f0727e86267cdfd4bac5fe3f Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 06:50:05 -0600 Subject: [PATCH 1/9] Update xts.R --- R/xts.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/xts.R b/R/xts.R index 877df2473e..994b270b83 100644 --- a/R/xts.R +++ b/R/xts.R @@ -9,9 +9,9 @@ as.data.table.xts <- function(x, keep.rownames = TRUE, ...) { as.xts.data.table <- function(x, ...) { stopifnot(requireNamespace("xts"), !missing(x), is.data.table(x)) - if (!any((index_class <- class(x[[1L]])) %in% c("POSIXct","Date"))) stop("data.table must have a POSIXct, Date or IDate column on first position, use `setcolorder` function.") - colsNumeric = vapply_1b(x, is.numeric)[-1L] # exclude first col, xts index - 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=if ("IDate" %in% index_class) as.Date(x[[1L]]) else x[[1L]]) + if (!xts::is.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 = vapply_1b(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]]) + return(xts::as.xts(r, order.by = x[[1L]])) } From 6fd94ca5a1967d59a69a0deea80aaac0b179bea1 Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 06:53:16 -0600 Subject: [PATCH 2/9] Update xts.R --- R/xts.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/xts.R b/R/xts.R index 994b270b83..5cc087875a 100644 --- a/R/xts.R +++ b/R/xts.R @@ -10,7 +10,7 @@ as.data.table.xts <- function(x, keep.rownames = TRUE, ...) { as.xts.data.table <- function(x, ...) { stopifnot(requireNamespace("xts"), !missing(x), is.data.table(x)) if (!xts::is.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 = vapply_1b(x, is.numeric)[-1L] + colsNumeric = vapply_1b(x, is.numeric)[-1L] # exclude first col, xts index 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]]) return(xts::as.xts(r, order.by = x[[1L]])) From 2522872cea7ae56197e9143fb7a44aacde66a15d Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 07:21:10 -0600 Subject: [PATCH 3/9] Update xts.R added support for IDate back --- R/xts.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/xts.R b/R/xts.R index 5cc087875a..142e561d3d 100644 --- a/R/xts.R +++ b/R/xts.R @@ -13,5 +13,5 @@ as.xts.data.table <- function(x, ...) { colsNumeric = vapply_1b(x, is.numeric)[-1L] # exclude first col, xts index 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]]) - return(xts::as.xts(r, order.by = x[[1L]])) + return(xts::as.xts(r, order.by = order.by = if ("IDate" %in% class(x[[1L]])) as.Date(x[[1L]]) else x[[1L]])) } From 9a31183d38987eb51aceae5bf109b678db3a1eb9 Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 08:08:36 -0600 Subject: [PATCH 4/9] added test for various xts::to.period scenarios --- inst/tests/tests.Rraw | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3e112e807d..9564faf788 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -6188,6 +6188,21 @@ if ("package:xts" %in% search()) { xt_dt = as.xts.data.table(dt) test(1465.3, all.equal(dt, dt_xt, check.attributes = FALSE)) test(1465.4, xt, xt_dt) + # index types returned from to.period + dt = data.table(index = as.Date((as.Date("2014-12-12") - 729):as.Date("2014-12-12"), origin = "1970-01-01"), quantity = as.numeric(rep(c(1:5), 73)), value = rep(c(1:73) * 100, 5)) + xt = as.xts(matrix(data = c(dt$quantity, dt$value), ncol = 2, dimnames = list(NULL, c("quantity", "value"))), order.by = dt$index) + xt_w = xts::to.weekly(xt) + xt_dt_xt_w = as.xts.data.table(as.data.table(xt_w)) + xt_m = xts::to.monthly(xt) + xt_dt_xt_m = as.xts.data.table(as.data.table(xt_m)) + xt_q = xts::to.quarterly(xt) + xt_dt_xt_q = as.xts.data.table(as.data.table(xt_q)) + xt_y = xts::to.yearly(xt) + xt_dt_xt_y = as.xts.data.table(as.data.table(xt_y)) + test(1465.5, xt_w, xt_dt_xt_w) + test(1465.6, xt_m, xt_dt_xt_m) + test(1465.7, xt_q, xt_dt_xt_q) + test(1465.8, xt_y, xt_dt_xt_y) } # as.data.table.default #969 From 1a7145afb9835116b849d0aa12ee7aea95222b81 Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 08:19:38 -0600 Subject: [PATCH 5/9] added fix for #2408 --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index e1bd2d672a..d3f44f2ed0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -92,6 +92,8 @@ 18. Integer values that are too large to fit in `int64` will now be read as strings [#2250](https://github.com/Rdatatable/data.table/issues/2250). 19. Internal-only `.shallow` now retains keys correctly, [#2336](https://github.com/Rdatatable/data.table/issues/2336). Thanks to @MarkusBonsch for reporting, fixing ([PR #2337](https://github.com/Rdatatable/data.table/pull/2337)) and adding 37 tests. This much advances the journey towards exporting `shallow()`, [#2323](https://github.com/Rdatatable/data.table/issues/2323). + +20. data.table.to.xts now supports all index classes supported by xts.Thnaks to @ebs238. closes [#2408](https://github.com/Rdatatable/data.table/issues/2408) #### NOTES From cee5f0b1d42e91acbd81529544e8cfd996820419 Mon Sep 17 00:00:00 2001 From: ebs238 Date: Fri, 13 Oct 2017 12:08:32 -0600 Subject: [PATCH 6/9] Update NEWS.md --- NEWS.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index d3f44f2ed0..e1bd2d672a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -92,8 +92,6 @@ 18. Integer values that are too large to fit in `int64` will now be read as strings [#2250](https://github.com/Rdatatable/data.table/issues/2250). 19. Internal-only `.shallow` now retains keys correctly, [#2336](https://github.com/Rdatatable/data.table/issues/2336). Thanks to @MarkusBonsch for reporting, fixing ([PR #2337](https://github.com/Rdatatable/data.table/pull/2337)) and adding 37 tests. This much advances the journey towards exporting `shallow()`, [#2323](https://github.com/Rdatatable/data.table/issues/2323). - -20. data.table.to.xts now supports all index classes supported by xts.Thnaks to @ebs238. closes [#2408](https://github.com/Rdatatable/data.table/issues/2408) #### NOTES From 2091cae3583517fa91589f7dd0638249791c8c8b Mon Sep 17 00:00:00 2001 From: ebs238 Date: Sat, 14 Oct 2017 15:43:38 -0600 Subject: [PATCH 7/9] Update xts.R --- R/xts.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/xts.R b/R/xts.R index 142e561d3d..9fe83123b6 100644 --- a/R/xts.R +++ b/R/xts.R @@ -13,5 +13,5 @@ as.xts.data.table <- function(x, ...) { colsNumeric = vapply_1b(x, is.numeric)[-1L] # exclude first col, xts index 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]]) - return(xts::as.xts(r, order.by = order.by = if ("IDate" %in% class(x[[1L]])) as.Date(x[[1L]]) else x[[1L]])) + return(xts::as.xts(r, order.by = if ("IDate" %in% class(x[[1L]])) as.Date(x[[1L]]) else x[[1L]])) } From 7eaaf5ae0bc39127824756aa0ea917b6894eeabb Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Sat, 14 Oct 2017 16:31:43 -0600 Subject: [PATCH 8/9] Update tests.Rraw --- inst/tests/tests.Rraw | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index e9e56a5a4c..8a15981df6 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -6201,10 +6201,10 @@ if ("package:xts" %in% search()) { xt_dt_xt_q = as.xts.data.table(as.data.table(xt_q)) xt_y = xts::to.yearly(xt) xt_dt_xt_y = as.xts.data.table(as.data.table(xt_y)) - test(1465.5, xt_w, xt_dt_xt_w) - test(1465.6, xt_m, xt_dt_xt_m) - test(1465.7, xt_q, xt_dt_xt_q) - test(1465.8, xt_y, xt_dt_xt_y) + test(1465.5, all.equal(xt_w, xt_dt_xt_w, check.attributes = FALSE)) + test(1465.6, all.equal(xt_m, xt_dt_xt_m, check.attributes = FALSE)) + test(1465.7, all.equal(xt_q, xt_dt_xt_q, check.attributes = FALSE)) + test(1465.8, all.equal(xt_y, xt_dt_xt_y, check.attributes = FALSE)) } # as.data.table.default #969 From f785157e8ecf5b441770d24812155a33dec6a66a Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Sat, 14 Oct 2017 16:56:15 -0600 Subject: [PATCH 9/9] Update NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index f25d3eec68..6f3138180f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -92,6 +92,8 @@ 17. Internal-only `.shallow` now retains keys correctly, [#2336](https://github.com/Rdatatable/data.table/issues/2336). Thanks to @MarkusBonsch for reporting, fixing ([PR #2337](https://github.com/Rdatatable/data.table/pull/2337)) and adding 37 tests. This much advances the journey towards exporting `shallow()`, [#2323](https://github.com/Rdatatable/data.table/issues/2323). 18. `isoweek` calculation is correct regardless of local timezone setting (`Sys.timezone()`), [#2407](https://github.com/Rdatatable/data.table/issues/2407). Thanks to @MoebiusAV and @SimonCoulombe for reporting and @MichaelChirico for fixing. + +19. fixed `as.xts.data.table` to support all xts supported time based index clasess [#2408](https://github.com/Rdatatable/data.table/issues/2408). Thanks to @ebs238 for reporting and for the PR. #### NOTES