You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document as.IDate() behaviour with fractional dates #7952
as.IDate() calls as.integer() on Date input which means fractional dates are rounded towards the unix epoch. This leads to the following behaviour.
(origin<- .Date(0))
#> [1] "1970-01-01"
(dates<- c(origin-0.2, origin+0.2))
#> [1] "1969-12-31" "1970-01-01"data.table::as.IDate(dates)
#> [1] "1970-01-01" "1970-01-01"# I believe this is being done internally with the 'Date' method
as.integer(dates)
#> [1] 0 0# Alternative is to floor first
as.integer(floor(unclass(dates)))
#> [1] -1 0
To me flooring is the more 'expected' result so (assuming it is not something you wish to change) is it worth pointing this behaviour out explicitly in the documentation. Apologies if I've missed this but couldn't see it mentioned.
you are right. print.Date calls as.POSIXlt which floors internally, but this is undocument behavior.
The fix is probably to use x = as.integer(if (is.integer(x)) x else floor(unclass(x))) in as.IDate.Date and as.IDate.numeric to avoid copying to dbl and back for already int.
@ben-schwen - Just to flag before anyone picks this up. I think the flooring will have a small performance penalty. I suspect that fractional dates prior to the epoch are rarely encountered, so whether the behaviour is worth fixing (over just documentation) will want considering.
Hey @ben-schwen , I'd like to take this issue. Before starting can you clarify which approach you'd prefer, using floor() before converting to int in both the methods or keeping the current behaviour and document it ?
I'm happy to proceed with whichever approach makes more sense for the package.
@simplymayank I would prefer the flooring approach. Maybe something easy as:
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
}
Please also measure speed but AFAICT the difference will be neglectable. If we want to strive for speed after all, we could write floor_to_int as C function.
Activity
ben-schwen commentedon Nov 3, 2026
Hey Tim,
you are right.
print.Datecallsas.POSIXltwhich floors internally, but this is undocument behavior.The fix is probably to use
x = as.integer(if (is.integer(x)) x else floor(unclass(x)))inas.IDate.Dateandas.IDate.numericto avoid copying to dbl and back for already int.TimTaylor commentedon Nov 10, 2026
@ben-schwen - Just to flag before anyone picks this up. I think the flooring will have a small performance penalty. I suspect that fractional dates prior to the epoch are rarely encountered, so whether the behaviour is worth fixing (over just documentation) will want considering.
simplymayank commentedon Nov 12, 2026
Hey @ben-schwen , I'd like to take this issue. Before starting can you clarify which approach you'd prefer, using floor() before converting to int in both the methods or keeping the current behaviour and document it ?
I'm happy to proceed with whichever approach makes more sense for the package.
ben-schwen commentedon Nov 20, 2026
@simplymayank I would prefer the flooring approach. Maybe something easy as:
Please also measure speed but AFAICT the difference will be neglectable. If we want to strive for speed after all, we could write
floor_to_intas C function.