From 6096071a7fe40ee9557d53d02da962ceb04cc548 Mon Sep 17 00:00:00 2001 From: Mayank Gupta Date: Tue, 24 Nov 2026 18:59:16 +0530 Subject: [PATCH 1/4] Fix fractional date conversion in as.IDate --- R/IDateTime.R | 11 ++++++++--- inst/tests/tests.Rraw | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/R/IDateTime.R b/R/IDateTime.R index 471e197f02..749dfa6989 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -10,6 +10,11 @@ copy_names = function(ans, nm) { ans } +floor_to_int = function(x) { + if (is.double(x) && !inherits(x, "integer64")) as.integer(floor(unclass(x))) + else as.integer(x) +} + as.IDate.default = function(x, ..., tz = attr(x, "tzone", exact=TRUE)) { if (is.null(tz)) tz = "UTC" if (is.character(x)) { @@ -23,7 +28,7 @@ as.IDate.numeric = function(x, origin = "1970-01-01", ...) { if (origin=="1970-01-01") { # standard epoch nm = names(x) - x = as.integer(x) + x = floor_to_int(x) class(x) = c("IDate", "Date") # We used to use structure() here because class(x)<- copied several times in R before v3.1.0 # Since R 3.1.0 improved class()<- and data.table's oldest oldest supported R is now >3.1.0, we can use class<- again @@ -32,13 +37,13 @@ as.IDate.numeric = function(x, origin = "1970-01-01", ...) { copy_names(x, nm) } else { # only call expensive as.IDate.character if we have to - as.IDate(origin, ...) + copy_names(as.integer(x), names(x)) + as.IDate(origin, ...) + copy_names(floor_to_int(x), names(x)) } } as.IDate.Date = function(x, ...) { nm = names(x) - x = as.integer(x) # if already integer, x will be left unchanged as the original input + x = floor_to_int(x) # if already integer, x will be left unchanged as the original input class(x) = c("IDate", "Date") # class()<- will copy if as.integer() did not create, and may not if it did we hope copy_names(x, nm) } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3a446a8136..3161319367 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21867,6 +21867,12 @@ test(2379.12, as.IDate(d) + 1L, as.IDate(as.Date(d) + 1L)) test(2379.13, as.IDate("2020-01-01") + a, as.IDate(as.Date("2020-01-01") + a)) test(2379.14, as.IDate(d) - 1L, as.IDate(as.Date(d) - 1L)) +# #7952 as.IDate() handles fractional dates by rounding down +test(2379.15, as.IDate(as.Date("1969-12-31") + 0.5), as.IDate(-1L)) +test(2379.16, as.IDate(as.Date("1970-01-01") + 0.5), as.IDate(0L)) +test(2379.17, as.IDate(-0.5), as.IDate(-1L)) +test(2379.18, as.IDate(0.5), as.IDate(0L)) + # #6341: tstrsplit() supports reverse indexing test(2380.01, tstrsplit(c("ABC-DEF", "ABC-DEF-GHI", "ABC-DEF-GHI-JKL", "ABC-DEF-GHI-JKL-MNO"), "-", fixed=TRUE, keep=1:3, rev=TRUE), list(c("DEF", "GHI", "JKL", "MNO"), c("ABC", "DEF", "GHI", "JKL"), c(NA_character_, "ABC", "DEF", "GHI"))) test(2380.02, tstrsplit(c("A-B", "A-B-C"), "-", fixed=TRUE, rev=TRUE), list(c("B", "C"), c("A", "B"), c(NA_character_, "A"))) From 5e08ecc9a653ca5a6ead6fd83d8cb360d0d9b193 Mon Sep 17 00:00:00 2001 From: Mayank Gupta Date: Tue, 24 Nov 2026 19:55:42 +0530 Subject: [PATCH 2/4] Update fractional date tests --- inst/tests/tests.Rraw | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3161319367..849d9bbdef 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21868,10 +21868,10 @@ test(2379.13, as.IDate("2020-01-01") + a, as.IDate(as.Date("2020-01-01") + a)) test(2379.14, as.IDate(d) - 1L, as.IDate(as.Date(d) - 1L)) # #7952 as.IDate() handles fractional dates by rounding down -test(2379.15, as.IDate(as.Date("1969-12-31") + 0.5), as.IDate(-1L)) -test(2379.16, as.IDate(as.Date("1970-01-01") + 0.5), as.IDate(0L)) -test(2379.17, as.IDate(-0.5), as.IDate(-1L)) -test(2379.18, as.IDate(0.5), as.IDate(0L)) +origin = .Date(0) +dates = c(origin - 0.2, origin + 0.2) +test(2379.15, as.IDate(dates), c("1969-12-31", "1970-01-01")) +test(2379.16, as.IDate(c(-0.2, 0.2)), c("1969-12-31", "1970-01-01")) # #6341: tstrsplit() supports reverse indexing test(2380.01, tstrsplit(c("ABC-DEF", "ABC-DEF-GHI", "ABC-DEF-GHI-JKL", "ABC-DEF-GHI-JKL-MNO"), "-", fixed=TRUE, keep=1:3, rev=TRUE), list(c("DEF", "GHI", "JKL", "MNO"), c("ABC", "DEF", "GHI", "JKL"), c(NA_character_, "ABC", "DEF", "GHI"))) From be7b6736da2cd08988a6f66f2722f0562c467540 Mon Sep 17 00:00:00 2001 From: Mayank Gupta Date: Tue, 24 Nov 2026 20:22:10 +0530 Subject: [PATCH 3/4] Update fractional date handling and tests --- R/IDateTime.R | 4 ++-- inst/tests/tests.Rraw | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/IDateTime.R b/R/IDateTime.R index 749dfa6989..bfb2ed04a9 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -12,7 +12,7 @@ copy_names = function(ans, nm) { floor_to_int = function(x) { if (is.double(x) && !inherits(x, "integer64")) as.integer(floor(unclass(x))) - else as.integer(x) + else as.integer(x) # integer: no double round trip. integer64, complex, etc.: dispatch to their own methods } as.IDate.default = function(x, ..., tz = attr(x, "tzone", exact=TRUE)) { @@ -43,7 +43,7 @@ as.IDate.numeric = function(x, origin = "1970-01-01", ...) { as.IDate.Date = function(x, ...) { nm = names(x) - x = floor_to_int(x) # if already integer, x will be left unchanged as the original input + x = floor_to_int(x) # Convert to integer, rounding fractional values down class(x) = c("IDate", "Date") # class()<- will copy if as.integer() did not create, and may not if it did we hope copy_names(x, nm) } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 849d9bbdef..3819d1e5bf 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21870,8 +21870,8 @@ test(2379.14, as.IDate(d) - 1L, as.IDate(as.Date(d) - 1L)) # #7952 as.IDate() handles fractional dates by rounding down origin = .Date(0) dates = c(origin - 0.2, origin + 0.2) -test(2379.15, as.IDate(dates), c("1969-12-31", "1970-01-01")) -test(2379.16, as.IDate(c(-0.2, 0.2)), c("1969-12-31", "1970-01-01")) +test(2379.15, as.IDate(dates), as.IDate(c("1969-12-31", "1970-01-01"))) +test(2379.16, as.IDate(c(-0.2, 0.2)), as.IDate(c("1969-12-31", "1970-01-01"))) # #6341: tstrsplit() supports reverse indexing test(2380.01, tstrsplit(c("ABC-DEF", "ABC-DEF-GHI", "ABC-DEF-GHI-JKL", "ABC-DEF-GHI-JKL-MNO"), "-", fixed=TRUE, keep=1:3, rev=TRUE), list(c("DEF", "GHI", "JKL", "MNO"), c("ABC", "DEF", "GHI", "JKL"), c(NA_character_, "ABC", "DEF", "GHI"))) From 8aceb1733c56274690f4119ccb8bc169c8632802 Mon Sep 17 00:00:00 2001 From: Mayank Gupta Date: Wed, 25 Nov 2026 14:38:35 +0530 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index f74eed63a8..6689be0cf7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -96,6 +96,8 @@ 21. GForce grouping is again faster when running single-threaded, e.g. after `setDTthreads(1)`, [#3330](https://github.com/Rdatatable/data.table/issues/3330). The batching used to parallelize `gsum()` and `gmean()` is skipped when only one thread is available. Also, `mean()` of an `integer` or `logical` column by group no longer coerces the whole column to `double` first, which made it slower than in v1.12.0 regardless of the number of threads. Thanks @jangorecki for the benchmarks, @mattdowle for the report, and @ben-schwen for the fix. +22. `as.IDate()` now correctly handles fractional `Date` and numeric inputs by rounding down instead of truncating toward zero, [#7952](https://github.com/Rdatatable/data.table/issues/7952). Thanks to @TimTaylor for reporting, @ben-schwen for suggesting the approach, and @simplymayank for implementing the fix. + ### Notes 1. {data.table} now depends on R 3.5.0 (2018).