timetk icon indicating copy to clipboard operation
timetk copied to clipboard

Fix `future_frame()` failure for single observations

Open ezraporter opened this issue 2 years ago • 0 comments

Fixes #108

Allows tk_make_future_timeseries() to work with length one vectors which is the source of the future_frame() failure. Currently tk_make_future_timeseries() tries to infer the series frequency with the median time difference. For length one vectors this fix sets the frequency as one unit of the datatype (i.e. Date -> 1 day).

This also required an update to tk_get_timeseries_summary() which currently returns scale = "second" for all length 1 vectors.

Admittedly after implementing I'm not sure this is the best solution for future_frame() since it can cause some odd behavior if the frequency isn't really 1 unit of the datatype but you could only know it by looking across groups:

library(tidyverse)
library(timetk)

# weekly series
series <- tibble(
    group = c("A", "A", "B"),
    week = as.Date(c("2023-01-01", "2023-01-08", "2023-01-01"))
)

series |>
    group_by(group) |>
    future_frame(week, 4)

# group A comes out weekly but group B comes out daily
#> # A tibble: 8 × 2
#> # Groups:   group [2]
#> group week      
#> <chr> <date>    
#> 1 A     2023-01-15
#> 2 A     2023-01-22
#> 3 A     2023-01-29
#> 4 A     2023-02-05
#> 5 B     2023-01-02
#> 6 B     2023-01-03
#> 7 B     2023-01-04
#> 8 B     2023-01-05

ezraporter avatar Mar 03 '23 05:03 ezraporter