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

Consider "time [interval?] delta" helpers #494

Open
brookslogan opened this issue Jul 26, 2024 · 0 comments
Open

Consider "time [interval?] delta" helpers #494

brookslogan opened this issue Jul 26, 2024 · 0 comments

Comments

@brookslogan
Copy link
Contributor

brookslogan commented Jul 26, 2024

Context

Proposal

Consider implementing these helpers:

  • unit_time_delta(time_type): takes a time_type, outputs an object that, added to a time_value of kind time_type, will output the next possible time_value, and which is an acceptable before argument.
    • For "daily": either 1 or as.difftime(1, units = "days")
    • For "weekly": as.difftime(1, units = "weeks")
    • For yearmon: 1
    • etc.
    • This could be mostly S3 except for "daily" vs. "weekly" Dates.
    • (Maybe tack on a "time_delta" class so we "know" that we generated these results.)
    • (Maybe this needs a "robust/messaging" version and a "fast" version if outputting 1 and 7 for daily and weekly instead of difftimes is very much faster.)
  • n_periods_to_time_delta(x, time_type): outputs x * unit_time_delta(time_type) (note recycling rules should mean this works on length != 1 vectors)
  • time_delta_to_n_periods(x, time_type): reverse operation, but allowing for both the numeric and difftime as inputs for "daily", whereas in n_period_to_time_delta we'd just choose to output one or the other. (Or maybe be strict and have an as_time_delta convert to the strict format.)
  • {is,assert}_time_delta(x, time_type): validates whether x looks like it's an appropriate time difference. Like validate_slide_window_arg, but maybe allowing lengths other than 1, maybe allowing negatives, and maybe(??) disallowing Inf
  • [time_diff(time_values, time_type) (like diff()), time_sub(time_values1, time_values2, time_type) (like -)? with time_sub maybe catching mismatched wdays for "weekly" --- again, this is almost but not quite just S3 due to "weekly" Dates, and not sure about tsibble/clock classes]

Use cases:

  • before, after validation in non-Inf case
  • maybe making working with guess_period, complete, etc. easier
  • avoiding slow difftime operations
  • [potentially supersede next_after]
  • [potentially help clarify what type lag should be when we calculate it]

Discussion

@dshemetov @dsweber2 do you see these simplifying life at all?

[Originally, I was thinking of time_diff rather than time_delta, but time_delta seems more suggestive that this is something you can add to time values sensibly, while time_diff sounds like it would have to be the result of subtracting two time values, which isn't necessarily the case.]

[We should also consider n_steps instead of n_periods; might fit better for yearmon.

And maybe time_interval_delta, n_intervals?]

@brookslogan brookslogan changed the title Consider "time diff" helpers Consider "time [interval?] delta" helpers Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant