From ca2440f24853754b2c5238c1bf4d4886239eb9d3 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Thu, 11 Feb 2021 20:09:12 +0200 Subject: [PATCH] fixes coerce from xts where a column x was preent, closes #4897 --- NEWS.md | 2 ++ R/xts.R | 3 ++- inst/tests/tests.Rraw | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 1032017a00..7a9297085a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,8 @@ 2. `fintersect()` now retains the order of the first argument as reasonably expected, rather than retaining the order of the second argument, [#4716](https://github.com/Rdatatable/data.table/issues/4716). Thanks to Michel Lang for reporting, and Ben Schwen for the PR. +3. `as.data.table` method for `xts` objects could have return an incorrect xts index value, [#4897](https://github.com/Rdatatable/data.table/issues/4897). Thanks to @emilsjoerup for reporting. + ## NOTES 1. Compiling from source no longer requires `zlib` header files to be available, [#4844](https://github.com/Rdatatable/data.table/pull/4844). The output suggests installing `zlib` headers, and how (e.g. `zlib1g-dev` on Ubuntu) as before, but now proceeds with `gzip` compression disabled in `fwrite`. Upon calling `fwrite(DT, "file.csv.gz")` at runtime, an error message suggests to reinstall `data.table` with `zlib` headers available. This does not apply to users on Windows or Mac who install the pre-compiled binary package from CRAN. diff --git a/R/xts.R b/R/xts.R index bfb6f813a7..0082444bb2 100644 --- a/R/xts.R +++ b/R/xts.R @@ -8,7 +8,8 @@ as.data.table.xts = function(x, keep.rownames = TRUE, key=NULL, ...) { if (identical(keep.rownames, FALSE)) return(r[]) index_nm = if (is.character(keep.rownames)) keep.rownames else "index" if (index_nm %chin% names(x)) stop(gettextf("Input xts object should not have '%s' column because it would result in duplicate column names. Rename '%s' column in xts or use `keep.rownames` to change the index column name.", index_nm, index_nm, domain="R-data.table"), domain=NA) - r[, c(index_nm) := zoo::index(x)] + `__idx__` = zoo::index(x) + r[, c(index_nm) := `__idx__`] setcolorder(r, c(index_nm, setdiff(names(r), index_nm))) # save to end to allow for key=index_nm setkeyv(r, key) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index c5910f5c81..b1ba700d15 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -17263,3 +17263,11 @@ if (identical(x, enc2native(x))) { # fintersect now preserves order of first argument like intersect, #4716 test(2163, fintersect(data.table(x=c("b", "c", "a")), data.table(x=c("a","c")))$x, c("c", "a")) + +# as.data.table.xts(foo) gives wrong index values when `'x'` is in the column names. #4897 +if (test_xts) { + b = xts(cbind(1:3), as.POSIXct(101:103, origin="1970-01-01", tz="UTC"), dimnames=list(NULL, "x")) + expected = data.table(index=as.POSIXct(101:103, origin="1970-01-01", tz="UTC"), x=1:3) + attr(expected$index, "tclass") = class(expected$index) + test(2164.1, as.data.table(b), expected) +}