From 6de274277753f03ef6e59167e397438ccd04e930 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 4 Oct 2023 09:05:52 +0200 Subject: [PATCH 1/4] custom data.frame classes redirected to as.data.frame before as.data.table, #5699 --- R/as.data.table.R | 1 + inst/tests/tests.Rraw | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/R/as.data.table.R b/R/as.data.table.R index feae0f5ace..e8e5939c9b 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -214,6 +214,7 @@ as.data.table.list = function(x, } as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) { + if (!identical(class(x), "data.frame")) return(as.data.frame(x)) if (!isFALSE(keep.rownames)) { # can specify col name to keep.rownames, #575; if it's the same as key, # kludge it to 'rn' since we only apply the new name afterwards, #4468 diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 9117c0fcb6..4e87bacd4e 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18094,3 +18094,15 @@ test(2238.6, "a" %notin% integer(), TRUE) test(2238.7, "a" %notin% NULL, TRUE) test(2238.8, NA %notin% 1:5, TRUE) test(2238.9, NA %notin% c(1:5, NA), FALSE) + +# as.data.table should remove extra attributes from extended data.frames #5699 +x = data.frame(a=c(1,5,3), b=c(2,4,6)) +class(x) = c("tbl","data.frame") +attr(x, "t1") = "a" +as.data.frame.tbl = function(x) { + attr(x, "t1") = NULL + class(x) = "data.frame" + x +} +y = as.data.table(x) +test(2239.1, attr(y, "t1", TRUE), NULL) From 847541e4266f2cd6d31351e84c870c5ce43b589a Mon Sep 17 00:00:00 2001 From: Jan Gorecki Date: Wed, 4 Oct 2023 09:23:55 +0200 Subject: [PATCH 2/4] Missing call spotted by Michael --- R/as.data.table.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/as.data.table.R b/R/as.data.table.R index e8e5939c9b..e305293e56 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -214,7 +214,7 @@ as.data.table.list = function(x, } as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) { - if (!identical(class(x), "data.frame")) return(as.data.frame(x)) + if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x))) if (!isFALSE(keep.rownames)) { # can specify col name to keep.rownames, #575; if it's the same as key, # kludge it to 'rn' since we only apply the new name afterwards, #4468 From 96336637174de7e87fc0da9480b8cd4486151d7f Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 4 Dec 2024 07:31:28 +0000 Subject: [PATCH 3/4] amend test to work in/out of cc() --- inst/tests/tests.Rraw | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 8b81bf68a7..5bbea191b3 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -20667,12 +20667,11 @@ test(2299.12, data.table(a=list(data.table(b=1))), output="a\ # as.data.table should remove extra attributes from extended data.frames #5699 x = data.frame(a=c(1,5,3), b=c(2,4,6)) -class(x) = c("tbl","data.frame") +class(x) = c("tbl", "data.frame") attr(x, "t1") = "a" as.data.frame.tbl = function(x) { attr(x, "t1") = NULL class(x) = "data.frame" x } -y = as.data.table(x) -test(2300, attr(y, "t1", TRUE), NULL) +test(2300, attr(as.data.table(y), "t1"), attr(as.data.frame(y), "t1")) From ae8c6ff045e8abd89dc31e6e97432685e17bce60 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 17 Jan 2025 18:01:08 +0000 Subject: [PATCH 4/4] add NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 35806cf284..14972081d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -129,6 +129,8 @@ rowwiseDT( 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. +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. + ## NOTES 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).