Skip to content
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

Infinite dates are not parsed correctly #1458

Open
keesdeschepper opened this issue Jan 12, 2023 · 3 comments
Open

Infinite dates are not parsed correctly #1458

keesdeschepper opened this issue Jan 12, 2023 · 3 comments
Labels
bug an unexpected problem or unintended behavior datetime 📆

Comments

@keesdeschepper
Copy link
Contributor

What seems to be new in R is that infinite dates are formatted as "Inf" when coerced to string. This causes problems in write-read roundtrips, as readr's parser guessing does not accept "Inf" as a valid date:

library(tidyverse)
output <- format_csv(tibble(x = lubridate::as_date(c(0, Inf))))
output
#> [1] "x\n1970-01-01\nInf\n"
input <- suppressMessages(read_csv(output))
attr(input, "spec")
#> cols(
#>   x = col_character()
#> )

Assuming this is correct there are some solutions. Personally I would prefer a change on the read side, where read_delim etc. accept "Inf" and "-Inf" as valid dates. It violates iso8601, but it would be nice to preserve the distinction between missing dates and infinite dates.

If that's not an option, there are also solutions on the write side.

  1. output infinite dates as something more in line with iso8601 like "9999-13-00";
  2. output as NA (which is functionally as it was before)
  3. give an error if infinite dates are written

Some parameter to control this is of course possible, but the important choice then, in my view, is what the default option would be.

@jennybc
Copy link
Member

jennybc commented Jan 12, 2023

Do you have more specifics on what version of R or lubridate or readr or ?? had different behaviour?

@keesdeschepper
Copy link
Contributor Author

Yeah it is since R 4.2.0:
"Not strictly fixing a bug, format()ing and print()ing of non-finite Date and POSIXt values NaN and ±Inf no longer show as NA but the respective string, e.g., Inf, for consistency with numeric vector's behaviour, fulfilling the wish of (https://bugs.r-project.org/show_bug.cgi?id=18308)."

@hadley
Copy link
Member

hadley commented Jul 31, 2023

library(readr)
df <- data.frame(x = .Date(c(0, Inf)))
df
#>            x
#> 1 1970-01-01
#> 2        Inf

output <- format_csv(df)
cat(output)
#> x
#> 1970-01-01
#> Inf

read_csv(output, col_types = list())
#> # A tibble: 2 × 1
#>   x         
#>   <chr>     
#> 1 1970-01-01
#> 2 Inf

Created on 2023-07-31 with reprex v2.0.2

@hadley hadley added the bug an unexpected problem or unintended behavior label Jul 31, 2023
@hadley hadley changed the title Infinite dates are rendered as "Inf", which causes problems Infinite dates are not parsed correctly Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior datetime 📆
Projects
None yet
Development

No branches or pull requests

3 participants