timetk icon indicating copy to clipboard operation
timetk copied to clipboard

`future_frame()` fails on grouped data with one observation

Open spsanderson opened this issue 3 years ago • 4 comments

I have a script I run here: https://github.com/spsanderson/healthyverse_tsa

Today, there is only one record for my package TidyDensity. The record is below:

data_transformed_tbl %>% group_by(package) %>% filter(package == "TidyDensity")
# A tibble: 1 x 3
# Groups:   package [1]
  date       package     value_trans
  <date>     <chr>             <dbl>
1 2022-01-21 TidyDensity       0.179

When I try to use bind_rows to add a future_frame the process bombs out.

> data_transformed_tbl %>% 
  group_by(package) %>% 
  filter(package == "TidyDensity") %>% 
  bind_rows(future_frame(., .date_var = date, .length_out = horizon))

Error: Problem with `mutate()` column `nested.col`.
i `nested.col = purrr::map(...)`.
x error in evaluating the argument 'x' in selecting a method for function 'as_date': 'from' must be a finite number
i The error occurred in group 1: package = "TidyDensity".
Run `rlang::last_error()` to see where the error occurred.

Here is the rlang::last_error()

> rlang::last_error()
<error/dplyr:::mutate_error>
Problem with `mutate()` column `nested.col`.
i `nested.col = purrr::map(...)`.
x error in evaluating the argument 'x' in selecting a method for function 'as_date': 'from' must be a finite number
i The error occurred in group 1: package = "TidyDensity".
Backtrace:
  1. `%>%`(...)
 36. base::.handleSimpleError(...)
 37. dplyr:::h(simpleError(msg, call))
Run `rlang::last_trace()` to see the full context.

Here is the trance:

> rlang::last_trace()
<error/dplyr:::mutate_error>
Problem with `mutate()` column `nested.col`.
i `nested.col = purrr::map(...)`.
x error in evaluating the argument 'x' in selecting a method for function 'as_date': 'from' must be a finite number
i The error occurred in group 1: package = "TidyDensity".
Backtrace:
     x
  1. +-`%>%`(...)
  2. +-dplyr::bind_rows(., future_frame(., .date_var = date, .length_out = horizon))
  3. | \-rlang::list2(...)
  4. +-timetk::future_frame(., .date_var = date, .length_out = horizon)
  5. +-timetk:::future_frame.grouped_df(., .date_var = date, .length_out = horizon)
  6. | \-`%>%`(...)
  7. +-dplyr::group_by_at(., .vars = group_names)
  8. | \-dplyr:::manip_at(...)
  9. |   \-dplyr:::tbl_at_syms(.tbl, .vars, .include_group_vars = .include_group_vars)
 10. |     \-dplyr:::tbl_at_vars(tbl, vars, .include_group_vars = .include_group_vars)
 11. |       \-dplyr::tbl_vars(tbl)
 12. |         +-dplyr:::new_sel_vars(tbl_vars_dispatch(x), group_vars(x))
 13. |         | \-base::structure(...)
 14. |         \-dplyr:::tbl_vars_dispatch(x)
 15. +-tidyr::unnest(., cols = nested.col)
 16. +-dplyr::select(., -data)
 17. +-dplyr::mutate(...)
 18. +-dplyr:::mutate.data.frame(...)
 19. | \-dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
 20. |   +-base::withCallingHandlers(...)
 21. |   \-mask$eval_all_mutate(quo)
 22. +-purrr::map(...)
 23. | \-timetk:::.f(.x[[i]], ...)
 24. |   \-timetk:::future_framer(...)
 25. |     +-timetk::tk_make_future_timeseries(...)
 26. |     \-timetk:::tk_make_future_timeseries.Date(...)
 27. |       \-`%>%`(...)
 28. +-lubridate::as_date(.)
 29. +-timetk:::tk_make_future_timeseries.yearmon(...)
 30. | \-timetk:::make_sequential_timeseries_regular_freq(...)
 31. |   +-base::seq(...)
 32. |   \-base::seq.default(...)
 33. |     \-base::stop("'from' must be a finite number")
 34. +-base::.handleSimpleError(...)
 35. | \-base:::h(simpleError(msg, call))
 36. \-base::.handleSimpleError(...)
 37.   \-dplyr:::h(simpleError(msg, call))
<error/simpleError>
error in evaluating the argument 'x' in selecting a method for function 'as_date': 'from' must be a finite number

Data is attached.

cran_logs.csv

spsanderson avatar Jan 23 '22 13:01 spsanderson

I'll take a look. I'm pretty sure it's because future frame expects a time delta which requires at least 2 time stamps.

Nevertheless I'm sure a fix can be made to help.

My time is very limited. I welcome any PRs if you'd like to try a fix.

mdancho84 avatar Jan 23 '22 13:01 mdancho84

I'll try to fix

spsanderson avatar Jan 23 '22 13:01 spsanderson

Awesome. Just make a PR and let me know as you have questions.

mdancho84 avatar Jan 23 '22 13:01 mdancho84

I ran into this bug as well and came across this issue so I tried to implement a quick fix. As I mention in the PR I'm not sure my solution is the right one but may be better than erroring.

ezraporter avatar Mar 03 '23 05:03 ezraporter