Skip to content

Commit

Permalink
fixes coerce from xts where a column x was preent (#4898)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangorecki authored May 25, 2021
1 parent 77b20ae commit 0c1e9cd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@

17. Assigning a wrong-length or non-list vector to a list column could segfault, [#4166](https://github.com/Rdatatable/data.table/issues/4166) [#4667](https://github.com/Rdatatable/data.table/issues/4667) [#4678](https://github.com/Rdatatable/data.table/issues/4678) [#4729](https://github.com/Rdatatable/data.table/issues/4729). Thanks to @fklirono, Kun Ren, @kevinvzandvoort and @peterlittlejohn for reporting, and to Václav Tlapák for the PR.

18. `as.data.table()` on `xts` objects containing a column named `x` would return an `index` of type plain `integer` rather than `POSIXct`, [#4897](https://github.com/Rdatatable/data.table/issues/4897). Thanks to Emil Sjørup for reporting, and Jan Gorecki for the PR.

## NOTES

1. New feature 29 in v1.12.4 (Oct 2019) introduced zero-copy coercion. Our thinking is that requiring you to get the type right in the case of `0` (type double) vs `0L` (type integer) is too inconvenient for you the user. So such coercions happen in `data.table` automatically without warning. Thanks to zero-copy coercion there is no speed penalty, even when calling `set()` many times in a loop, so there's no speed penalty to warn you about either. However, we believe that assigning a character value such as `"2"` into an integer column is more likely to be a user mistake that you would like to be warned about. The type difference (character vs integer) may be the only clue that you have selected the wrong column, or typed the wrong variable to be assigned to that column. For this reason we view character to numeric-like coercion differently and will warn about it. If it is correct, then the warning is intended to nudge you to wrap the RHS with `as.<type>()` so that it is clear to readers of your code that a coercion from character to that type is intended. For example :
Expand Down
2 changes: 1 addition & 1 deletion R/xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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(domain=NA, 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))
r[, c(index_nm) := zoo::index(x)]
r[, c(index_nm) := zoo::index(x), env=list(x=x)]
setcolorder(r, c(index_nm, setdiff(names(r), index_nm)))
# save to end to allow for key=index_nm
setkeyv(r, key)
Expand Down
5 changes: 5 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -6714,6 +6714,10 @@ if (test_xts) {
" 6: 1970-01-07 6", " 7: 1970-01-08 7", " 8: 1970-01-09 8",
" 9: 1970-01-10 9", "10: 1970-01-11 10"))
options(old)

# as.data.table.xts(foo) had incorrect integer index with a column name called 'x', #4897
M = as.xts(matrix(1, dimnames=list("2021-05-23", "x")))
test(1465.19, inherits(as.data.table(M)$index,"POSIXct"))

Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = TRUE)
}
Expand Down Expand Up @@ -17700,3 +17704,4 @@ test(2190.9, DT[1:2, a:=call('sum', 1)], error="type 'language' cannot be coerce
DT = data.table(i1 = c(234L, 250L, 169L, 234L, 147L, 96L, 96L, 369L, 147L, 96L), i4 = c(79L, 113L, 270L, -121L, 113L, 113L, -121L, 179L, -228L, 113L), v = 0)
test(2191, DT[1:5, sum(v), by=.(i5 = 1:5 %% 2L), verbose=TRUE], data.table(i5=1:0, V1=c(0,0)), output="gforce")


0 comments on commit 0c1e9cd

Please sign in to comment.