-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: sub-day precision Date should be floored when treated as integer #116
Conversation
Thanks! I wonder if there is a faster way to do this, because it makes the conversion ~50% slower: days <- as.Date(rep(c(-1.1, -0.1, 0, 0.1, 1.1), 10000), origin = "1970-01-01")
f1 <- function(x) mode(x) <- "integer"
f2 <- function(x) .Date(as.integer(floor(as.numeric(x))))
bench::mark(f1(days), f2(days), check = FALSE)
|
I don't really understand why, but this is even faster than the original: days <- as.Date(rep(c(-1.1, -0.1, 0, 0.1, 1.1), 10000), origin = "1970-01-01")
f1 <- function(x) mode(x) <- "integer"
f2 <- function(x) .Date(as.integer(floor(as.numeric(x))))
f3 <- function(x) { cls <- class(x); x <- floor(as.numeric(x)); class(x) <- cls; x }
stopifnot(f2(days) == f3(days))
bench::mark(f1(days), f2(days), f3(days), check = FALSE)
|
Very slightly faster, still: f4 <- function(x) { cls <- class(x); x <- floor(unclass(x)); class(x) <- cls; x } |
Except that I forgot to convert to integer. /o\ Never mind then. Proper benchmark:
|
Thank you! |
Thanks for taking a look at this. I did a little digging and it seems that the time consuming part is mainly the In the polars package I did the processing on the Rust side. |
Same as apache/arrow-nanoarrow#674, duckdb/duckdb-r#981
Because the Date type in R allows sub-days, it must be rounded to -Inf before being forced to integerish.
Conventional rounding in the direction of zero would change the direction of rounding before and after Unix origin.