Skip to content

Commit 826260a

Browse files
add tests and news.md
1 parent 158b833 commit 826260a

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
3. `fread(keepLeadingZeros=TRUE)` now correctly parses dates with components with leading zeros as dates instead of strings, [#6851](https://github.com/Rdatatable/data.table/issues/6851). Thanks @TurnaevEvgeny for the report and @ben-schwen for the fix.
1616

17+
4. `as.data.table()` now properly handles keys: explicitly specified keys are set, while unspecified keys are cleared (returning NULL from `key()`), [#6859](https://github.com/Rdatatable/data.table/issues/6859). Thanks @brookslogan for the report and @Mukulyadav2004 for the fix.
18+
1719
## NOTES
1820

1921
1. Continued work to remove non-API C functions, [#6180](https://github.com/Rdatatable/data.table/issues/6180). Thanks Ivan Krylov for the PRs and for writing a clear and concise guide about the R API: https://aitap.codeberg.page/R-api/.

R/as.data.table.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,13 @@ as.data.table.list = function(x,
214214
}
215215

216216
as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
217-
if (is.data.table(x)) return(as.data.table.data.table(x,keep.rownames = keep.rownames, key = key, ...)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
217+
if (is.data.table(x)) return(as.data.table.data.table(x, ...)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
218218
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x), keep.rownames=keep.rownames, key=key, ...))
219219
if (!isFALSE(keep.rownames)) {
220220
# can specify col name to keep.rownames, #575; if it's the same as key,
221221
# kludge it to 'rn' since we only apply the new name afterwards, #4468
222222
if (is.character(keep.rownames) && identical(keep.rownames, key)) key='rn'
223-
ans = data.table(rn=rownames(x), x, keep.rownames=FALSE)
224-
if (!is.null(key)) setkeyv(ans, key)
223+
ans = data.table(rn=rownames(x), x, keep.rownames=FALSE, key=key)
225224
if (is.character(keep.rownames))
226225
setnames(ans, 'rn', keep.rownames[1L])
227226
return(ans)
@@ -246,18 +245,18 @@ as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
246245
ans
247246
}
248247

249-
as.data.table.data.table = function(x, keep.rownames = FALSE, key = NULL, ...) {
248+
as.data.table.data.table = function(x, keep.rownames, key, ...) {
250249
# as.data.table always returns a copy, automatically takes care of #473
251250
if (any(cols_with_dims(x))) { # for test 2089.2
252-
return(as.data.table.list(x, keep.rownames = keep.rownames, key = key, ...))
251+
return(as.data.table.list(x, keep.rownames = if(missing(keep.rownames)) FALSE else keep.rownames, key = if(missing(key)) NULL else key, ...))
253252
}
254253
x = copy(x) # #1681
255254
# fix for #1078 and #1128, see .resetclass() for explanation.
256255
setattr(x, 'class', .resetclass(x, "data.table"))
257-
if (!is.null(key)) {
256+
if (!missing(key)){
258257
setkeyv(x, key)
259258
} else {
260-
setkeyv(x, NULL)
259+
setattr(x, "sorted", NULL)
261260
}
262261
x
263262
}

inst/tests/tests.Rraw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21084,3 +21084,9 @@ test(2307, { capture.output(print(DT, class = TRUE, show.indices = TRUE)); TRUE
2108421084
dt = data.table(date=as.IDate(c(NA, "2014-12-05")))
2108521085
test(2308.01, fread("date\nNA\n2014-12-05", keepLeadingZeros=TRUE), dt)
2108621086
test(2308.02, fread("date\nNA\n2014-12-05", keepLeadingZeros=FALSE), dt)
21087+
21088+
# Test that as.data.table.data.table preserves key when explicitly specified but not when omitted
21089+
test(2309.01, data.frame(t = c(3:1,4:5), y = 1:5) %>% as.data.table(key = "t") %>% key(), "t")
21090+
test(2309.02, tibble::tibble(t = c(3:1,4:5), y = 1:5) %>% as.data.table(key = "t") %>% key(), "t")
21091+
test(2309.03, data.table(t = c(3:1,4:5), y = 1:5) %>% as.data.table(key = "t") %>% key(), "t")
21092+
test(2309.04, data.table(t = c(3:1,4:5), key = "t") %>% as.data.table() %>% key(), NULL)

man/as.data.table.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Functions to check if an object is \code{data.table}, or coerce it if possible.
2121
\usage{
2222
as.data.table(x, keep.rownames=FALSE, \dots)
2323

24-
\method{as.data.table}{data.table}(x, keep.rownames=FALSE, key=NULL, \dots)
24+
\method{as.data.table}{data.table}(x, \dots)
2525

2626
\method{as.data.table}{array}(x, keep.rownames=FALSE, key=NULL, sorted=TRUE,
2727
value.name="value", na.rm=TRUE, \dots)

0 commit comments

Comments
 (0)