Skip to content

Commit 1eec7f3

Browse files
custom data.frame classes redirected to as.data.frame before as.data.table (#5700)
* custom data.frame classes redirected to as.data.frame before as.data.table, #5699 * Missing call spotted by Michael * amend test to work in/out of cc() * add NEWS --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent d263924 commit 1eec7f3

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ rowwiseDT(
129129

130130
17. Assignment with `:=` to an S4 slot of an under-allocated data.table now works, [#6704](https://github.com/Rdatatable/data.table/issues/6704). Thanks @MichaelChirico for the report and fix.
131131

132+
18. `as.data.table()` method for `data.frame`s (especially those with extended classes) is more consistent with `as.data.frame()` with respect to rention of attributes, [#5699](https://github.com/Rdatatable/data.table/issues/5699). Thanks @jangorecki for the report and fix.
133+
132134
## NOTES
133135

134136
1. There is a new vignette on joins! See `vignette("datatable-joins")`. Thanks to Angel Feliz for authoring it! Feedback welcome. This vignette has been highly requested since 2017: [#2181](https://github.com/Rdatatable/data.table/issues/2181).

R/as.data.table.R

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

216216
as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
217+
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x)))
217218
if (!isFALSE(keep.rownames)) {
218219
# can specify col name to keep.rownames, #575; if it's the same as key,
219220
# kludge it to 'rn' since we only apply the new name afterwards, #4468

inst/tests/tests.Rraw

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20742,3 +20742,14 @@ test(2301.1, DT[order(a, method="auto")], error="no support for sorting by metho
2074220742
test(2301.2, DT[order(a, b, decreasing=c(TRUE, FALSE))], DT[order(-a, b)])
2074320743
test(2301.3, DT[order(a, -b, decreasing=c(TRUE, TRUE))], error="Mixing '-' with vector decreasing")
2074420744
test(2301.4, DT[order(a, b, decreasing=c(TRUE, TRUE, FALSE))], error="decreasing= has length 3")
20745+
20746+
# as.data.table should remove extra attributes from extended data.frames #5699
20747+
x = data.frame(a=c(1,5,3), b=c(2,4,6))
20748+
class(x) = c("tbl", "data.frame")
20749+
attr(x, "t1") = "a"
20750+
as.data.frame.tbl = function(x) {
20751+
attr(x, "t1") = NULL
20752+
class(x) = "data.frame"
20753+
x
20754+
}
20755+
test(2302, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1"))

0 commit comments

Comments
 (0)