Skip to content

Coercion of a duration into a character and back does not work #1005

Closed

Description

@tvatter

Hi,

While coercing a duration into a character work, the output of the coercion into a character can't be coerced back into a duration.

(x <- duration(1, "years"))  # [1] "31557600s (~1 years)"
(y <- as.character(x))         # [1] "31557600s (~1 years)"
(z <- as.duration(y))           # [1] NA

I understand that the basic purpose of the parser is different and that this behavior is expected. However, I just encountered a situation where this is problematic:

df <- tibble(x = duration(1, "years"))     # x is a duration
tmp <- tempfile(); write_csv(df, tmp)    # write the df to file
df2 <- read_csv(tmp)                           # x is now a character which can't be easily coerced

Now, I get that one could just convert a duration as a numeric before writing, via mutate(across(where(is.duration), as.numeric)) or the "old" way mutate_if(is.duration, as.numeric). But it seems like an unnecessary burden to the user, especially since this behavior is not, to the best of my knowledge, documented.

I will also file a bug report in readr. While I understand that readr can't be expected to know whether to coerce to numeric or character for every object class out there, I would have expected that classes from the tidyverse packages should be supported seamlessly. However, it seems that the "responsibility" of providing a parser for the duration class should be on lubridate.

Thoughts?

Activity

  1. tvatter commented on Nov 17, 2021

    @tvatter
    Author

    Edit, I see that this isn't a new problem with readr:

  2. surdina commented on Jan 11, 2022

    @surdina

    I just came across the same thing!

    Perhaps rather naively, it also seems to me that as.duration() should be able to handle the string format it produces. The information is still there, and the coercion could be achieved by reading the string and ignoring everything after s.

    Right now, for character input, as.duration() calls as.period(), which calls parse_period(), which calls C_parse_period(). It's basically attempting to convert the duration column in string form to a period column and back.

    Would it make sense to check if the string is in that non-numeric display duration format? For instance inside of as.duration() in case as.period() yields NA, or -- if that's too slow -- within C_parse_period().

    Edit: Easy ad-hoc workaround for now:

    parse_duration <- function(x) {
        as.duration(gsub("\\s\\((.*)", "", x))
    }
    

    Applied to the example above:

    (x <- duration(1, "years")  # [1] "31557600s (~1 years)"
    (y <- as.character(x))      # [1] "31557600s (~1 years)"
    (z <- as.duration(y))       # [1] NA
    (z <- parse_duration(y))    # [1] "31557600s (~1 years)"
    (is.duration(z))            # [1] TRUE
    
  3. vspinu commented on Jan 12, 2022

    @vspinu
    Member

    Thanks or bringing it up.

  4. added a commit that references this issue on May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions