-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Evaluation of datetimes not as expected #536
Comments
This is an unintuitive and frustrating behavior, but unfortunately the source of it is not a bug in {pointblank}. Rather, this has to do with the side effect of {lubridate} sanitizing date/datetime comparison operations upon Indeed, in base R, comparing an older datetime with newer date (almost always) returns FALSE due to the technicality that dates and datetimes are represented as integers - in seconds and in days, respectively. In turn, old_datetime <- as.POSIXct("2021-01-01 00:15:00")
current_date <- Sys.Date()
old_datetime < current_date
#> Warning: Incompatible methods ("Ops.POSIXt", "Ops.Date") for "<"
#> [1] FALSE
unclass(old_datetime)
#> [1] 1609478100
#> attr(,"tzone")
#> [1] ""
unclass(current_date)
#> [1] 19881 It's only when using library(lubridate)
old_datetime < current_date
#> [1] TRUE I'm not sure, on principle, whether {pointblank} should also do the {lubridate}-style auto-magic (@rich-iannone do you have any thoughts?), but in generally I would strongly advise against the expectation that you can safely compare date and date time objects in R, as they are values of disparate types. With that in mind, and at least as an immediate fix, I would suggest the code in your reprex to use values that match the type of the column its being compared to (the datetime create_agent(df) |>
col_vals_lte(datetime, lubridate::now()) |>
col_vals_lte(date, lubridate::today()) |>
interrogate() In practice, outside of this toy example, you may need to cast date values into datetime when comparing it to datetime columns and vice versa. |
Thank you @yjunechoe ! I had suspected the different object types were to blame, and your explanation is very valuable. It is useful in the base R approach that a warning is surfaced. If pointblank maintains alignment with the base R approach for comparing disparate types, surfacing that warning would be very helpful indeed. But for now, your workaround is exactly what I needed too. Thank you!! |
Good to hear that the workaround works for your usecase! And to your point:
I 100% agree, and I think that that specific part may be a bug (thanks!!) - I've opened a new issue for this. And actually, I suspect that fixing #537 should just give us the lubridate behavior when that's loaded - will update back here after some experimenting! |
Thanks again for the report! Dev version now does the more sensible thing of inheriting the # remotes::install_github("rstudio/pointblank")
library(pointblank)
library(dplyr)
library(lubridate)
df <- tibble(datetime = as.POSIXct(c("2021-01-01 00:15:00",
"2023-09-29 03:35:00",
"2023-12-16 06:51:00",
"2024-02-28 00:15:00"))) |>
mutate(date=as_date(datetime))
create_agent(df) |>
col_vals_lte(datetime, today()) |>
col_vals_lte(date, today()) |>
interrogate() |
Prework
Description
Running a validation that a datetime is less than / greater than lubridate::today() yields opposite results, i.e., a date that is prior to/less than today evaluates as a fail under
col_vals_lt
and vice versa.I know it's comparing a
datetime
to adate
, but I wouldn't expect to have to do a conversion to run a test such as this, and I wouldn't expect it to appear to work but provide incorrect results.Reproducible example
The validation on
![image](https://private-user-images.githubusercontent.com/32964481/337662515-a149fd06-33a6-48ca-ac28-788f5dedddf1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk5MzIzNzIsIm5iZiI6MTczOTkzMjA3MiwicGF0aCI6Ii8zMjk2NDQ4MS8zMzc2NjI1MTUtYTE0OWZkMDYtMzNhNi00OGNhLWFjMjgtNzg4ZjVkZWRkZGYxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE5VDAyMjc1MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIzMjg3ZTdhMTc0ODQzZjkxMjcxZjQxZjdlODc1YTg3MDE0MTM5ODIwMjc5YTk0ZDNjZDRiODU5NjRhOTdjNjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.7HmWlgK0XroHc7JsGyhDIlXcF4GcpDxEkQW5OnYsZQQ)
datetime
failed all butdate
passed all.Expected result
Validation on the datetime should have passed all dates less than today.
Session info
Created on 2024-06-07 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: