Closed ad1729 closed 1 year ago
I am also puzzled by this behavior. Internally in as_date
we were using strptime
which does seem to support multiple formats, though it's not documented, but then it breaks inside dplyr across. So I have moved away from strptime
to parse_date_time(exact = TRUE)
to have a more predictable behavior and explicitly allow multiple formats in format argument.
Thanks for looking into this and fixing it!
Did you return to strptime
in version 1.9.1?
I had to change the following code from prior to 1.9.0 as_date("Jan 16 2023", format = "%b %d %Y")
to as_date("Jan 16 2023", format = "bdY")
to get the same output in 1.9.0 but now in 1.9.1 format = "bdY"
produces NA and format = "%b %d %Y"
produces the correct output once more.
@canadice No, the code in 1.9.0 was a regression. We didn't return to strtime, but parse with exact = TRUE
to comply with as.Date
. Also as_datetime
and as_date
now behave similarly. For instance multiple formats can be passed.
Change introduced in 3aa948d59103a3d5cf882cc725601bc5a13de797
@canadice No, the code in 1.9.0 was a regression. We didn't return to strtime, but parse with
exact = TRUE
to comply withas.Date
. Alsoas_datetime
andas_date
now behave similarly. For instance multiple formats can be passed.Change introduced in 3aa948d
Right, and the changes I made when updating to 1.9.0 seemed to work but now with 1.9.1 they have reversed.
To clarify:
<1.9.0 lubridate
as_date("Jan 16 2023", format = "%b %d %Y")
produces no errors
1.9.0 lubridate
as_date("Jan 16 2023", format = "%b %d %Y")
produces NA
as_date("Jan 16 2023", format = "bdY")
produces no errors so changed to this way of writing format
.
1.9.1 lubridate
as_date("Jan 16 2023", format = "%b %d %Y")
produces no errors
as_date("Jan 16 2023", format = "bdY")
produces NA
When troubleshooting the NAs in 1.9.0 I detailed it to the parse_date_time(exact = TRUE)
and made appropriate changes, but now those changes does not work and the "old" format can be used again. Is this an intended feature with the new version (using "%b %d %Y"
) or something that will be reverted again, i.e. any changes I do will have to be redone again to the 1.9.0 solutions I had?
yes, the exact = TRUE
means that you need to specify the exact format (like in strptime
) and not lubridate lax format (aka orders). So this particular behavior now is as in <1.9.0.
When troubleshooting the NAs in 1.9.0 I detailed it to the parse_date_time(exact = TRUE) and made appropriate changes, but now those changes does not work
If you used parse_date_time(exact = TRUE)
you should not be affected by any of this. This change affects only as_date
.
Hi,
I have a data frame with multiple date columns (encoded as characters) where the date format can differ between columns (e.g.
col1
has the following formatYYYY-MM-DD
andcol2
is coded asDD/MM/YYYY
. What I'd like to do is supply multiple formats while converting the char columns to date columns.Using
lubridate::as_date(..., format = c(...))
, lubridate version is 1.8.0Not sure why this fails to convert the first element in date2 from
03/10/2020
to2020-10-03
.On the other hand, using base R's
as.Date(..., tryFormats = c(...))
works as expected / desired.Additionally, using
as_date(parse_date_time(..., orders = c(...)))
gives the right result too on this example.What is unexpected / surprising is that
as_date(...)
accepts multiple formats but generatesNA
s where there should be data. As far as I can tell, this only seems to be happening when combiningas_date
andacross
. If there are multiple formats in one single column thenas_date
works as expected