Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
11 changes: 8 additions & 3 deletions R/IDateTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) # integer: no double round trip. integer64, complex, etc.: dispatch to their own methods
}

as.IDate.default = function(x, ..., tz = attr(x, "tzone", exact=TRUE)) {
if (is.null(tz)) tz = "UTC"
if (is.character(x)) {
Expand All @@ -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
Expand All @@ -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) # 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)
}
Expand Down
6 changes: 6 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -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
origin = .Date(0)
dates = c(origin - 0.2, origin + 0.2)
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")))
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")))
Expand Down
Loading