Skip to content

Precision on date_group() is clamped at max value for unit instead of performing numerical conversion when n > max #359

@matiasandina

Description

@matiasandina

I am not sure if this is by design or not, but I really like the behavior of date_group() and it just exploded in my face when trying to use it with more than 60 minutes. Here's a reproducible example:

now <- Sys.time()
dts <- clock::date_seq(from = now, to =  now + lubridate::hours(5), by = 600)
tibble::tibble(dts = dts,
               min_60 = clock::date_group(dts, precision = "minute", n = 60),
               # gets clamped at 1 hour mark
               min_120 = clock::date_group(dts, precision = "minute", n = 120),
               h_2 = clock::date_group(dts, precision = "hour", n = 2)
)

# A tibble: 31 × 4
   dts                 min_60              min_120             h_2                
   <dttm>              <dttm>              <dttm>              <dttm>             
 1 2023-08-11 16:38:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 2 2023-08-11 16:48:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 3 2023-08-11 16:58:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 4 2023-08-11 17:08:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
 5 2023-08-11 17:18:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
 6 2023-08-11 17:28:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
 7 2023-08-11 17:38:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
 8 2023-08-11 17:48:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
 9 2023-08-11 17:58:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00
10 2023-08-11 18:08:29 2023-08-11 18:00:00 2023-08-11 18:00:00 2023-08-11 18:00:00
# ℹ 21 more rows
# ℹ Use `print(n = ...)` to see more rows

It would be nice for clock to automatically perform the conversion. If difficult to change, I think there should be some sort of verbose warning to the user, since the grouping gets performed silently and gives the illusion of it working properly.

This gets properly handled as I would expect with date_floor (conversion being made), so I'm not sure if date_group() is doing what it is supposed to be doing and I should be using date_floor instead.

tibble::tibble(dts = dts,
               min_60 = clock::date_group(dts, precision = "minute", n = 60),
               # gets clamped at 1 hour mark
               min_120 = clock::date_group(dts, precision = "minute", n = 120),
               h_2 = clock::date_group(dts, precision = "hour", n = 2),
               floor = clock::date_floor(dts, precision = "minute", n = 120)
)

# A tibble: 31 × 5
   dts                 min_60              min_120             h_2                 floor              
   <dttm>              <dttm>              <dttm>              <dttm>              <dttm>             
 1 2023-08-11 16:38:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 2 2023-08-11 16:48:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 3 2023-08-11 16:58:29 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 4 2023-08-11 17:08:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 5 2023-08-11 17:18:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 6 2023-08-11 17:28:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 7 2023-08-11 17:38:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 8 2023-08-11 17:48:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
 9 2023-08-11 17:58:29 2023-08-11 17:00:00 2023-08-11 17:00:00 2023-08-11 16:00:00 2023-08-11 16:00:00
10 2023-08-11 18:08:29 2023-08-11 18:00:00 2023-08-11 18:00:00 2023-08-11 18:00:00 2023-08-11 18:00:00
# ℹ 21 more rows
# ℹ Use `print(n = ...)` to see more rows

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