Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions R/get-tk_get_timeseries.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,22 @@ tk_get_timeseries_summary <- function(idx) {

#' @export
tk_get_timeseries_summary.POSIXt <- function(idx) {
get_timeseries_summary_date(idx)
get_timeseries_summary_date(idx, type = "POSIXt")
}

#' @export
tk_get_timeseries_summary.Date <- function(idx) {
get_timeseries_summary_date(idx)
get_timeseries_summary_date(idx, type = "Date")
}

#' @export
tk_get_timeseries_summary.yearmon <- function(idx) {
get_timeseries_summary_date(idx)
get_timeseries_summary_date(idx, type = "yearmon")
}

#' @export
tk_get_timeseries_summary.yearqtr <- function(idx) {
get_timeseries_summary_date(idx)
get_timeseries_summary_date(idx, type = "yearqtr")
}

#' @export
Expand All @@ -169,7 +169,7 @@ tk_get_timeseries_summary.default <- function(idx) {
stop(paste0("No method for class ", class(idx)[[1]], "."))
}

get_timeseries_summary_date <- function(idx) {
get_timeseries_summary_date <- function(idx, type = NULL) {



Expand All @@ -183,15 +183,38 @@ get_timeseries_summary_date <- function(idx) {
) %>%
purrr::map_df(~ .x)

suppressWarnings(idx_periodicity <- xts::periodicity(idx))

idx_period_summary <- tibble::tibble(
start = idx_periodicity$start,
end = idx_periodicity$end,
units = idx_periodicity$units,
scale = idx_periodicity$label
# label = idx_periodicity$label
)
if (length(idx) == 1) {
units <- switch(
type,
"POSIXt" = "secs",
"Date" = "days",
"yearmon" = "days",
"yearqtr" = "days"
)
scale <- switch(
type,
"POSIXt" = "second",
"Date" = "day",
"yearmon" = "month",
"yearqtr" = "quarter"
)
idx_period_summary <- tibble::tibble(
start = idx,
end = idx,
units = units,
scale = scale
)
} else {
suppressWarnings(idx_periodicity <- xts::periodicity(idx))

idx_period_summary <- tibble::tibble(
start = idx_periodicity$start,
end = idx_periodicity$end,
units = idx_periodicity$units,
scale = idx_periodicity$label
# label = idx_periodicity$label
)
}

idx_nobs_summary <- tibble::tibble(
n.obs = length(idx)
Expand Down
84 changes: 65 additions & 19 deletions R/make-tk_make_timeseries_future.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ tk_make_future_timeseries.POSIXt <- function(idx, length_out, inspect_weekdays =
tzone <- lubridate::tz(idx)
lubridate::tz(idx) <- "UTC"

# If idx is length 1 explicitly pass frequency
if (length(idx) == 1) {
frequency <- 1
} else {
frequency <- NULL
}

# Handle Date formatted as date-time
if (idx_summary$scale %in% c("day", "week", "month", "quarter", "year")) {
idx <- lubridate::as_date(idx)
Expand All @@ -175,7 +182,8 @@ tk_make_future_timeseries.POSIXt <- function(idx, length_out, inspect_weekdays =
inspect_months = inspect_months,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
n_future = n_future,
frequency = frequency
)

} else {
Expand All @@ -184,7 +192,8 @@ tk_make_future_timeseries.POSIXt <- function(idx, length_out, inspect_weekdays =
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
n_future = n_future,
frequency = frequency
)
}

Expand Down Expand Up @@ -216,6 +225,13 @@ tk_make_future_timeseries.Date <- function(idx, length_out, inspect_weekdays = F

idx_summary <- tk_get_timeseries_summary(idx)

# If idx is length 1 explicitly pass frequency
if (length(idx) == 1) {
frequency <- 86400
} else {
frequency <- NULL
}

if (idx_summary$scale == "day" && (inspect_weekdays || inspect_months)) {

# print("day + inspect_weekdays or inspect_months")
Expand All @@ -224,13 +240,13 @@ tk_make_future_timeseries.Date <- function(idx, length_out, inspect_weekdays = F
tryCatch({

date_seq <- predict_future_timeseries_daily(
idx = idx,
length_out = length_out,
inspect_weekdays = inspect_weekdays,
inspect_months = inspect_months,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
idx = idx,
length_out = length_out,
inspect_weekdays = inspect_weekdays,
inspect_months = inspect_months,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
)

}, error = function(e) {
Expand All @@ -241,7 +257,8 @@ tk_make_future_timeseries.Date <- function(idx, length_out, inspect_weekdays = F
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
n_future = n_future,
frequency = frequency
)

})
Expand All @@ -256,7 +273,8 @@ tk_make_future_timeseries.Date <- function(idx, length_out, inspect_weekdays = F
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
n_future = n_future,
frequency = frequency
)

} else if (idx_summary$scale == "week") {
Expand All @@ -269,7 +287,8 @@ tk_make_future_timeseries.Date <- function(idx, length_out, inspect_weekdays = F
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future
n_future = n_future,
frequency = frequency
)

} else if (idx_summary$scale == "month") {
Expand Down Expand Up @@ -378,13 +397,20 @@ tk_make_future_timeseries.yearmon <- function(idx, length_out, inspect_weekdays
rlang::abort("Argument `length_out` is missing with no default")
}

# If idx is length 1 explicitly pass frequency
if (length(idx) == 1) {
frequency <- 1/12
} else {
frequency <- NULL
}

ret <- make_sequential_timeseries_regular_freq(
idx = idx,
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future)
n_future = n_future,
frequency = frequency)

return(ret)
}
Expand All @@ -408,12 +434,20 @@ tk_make_future_timeseries.yearqtr <- function(idx, length_out, inspect_weekdays
rlang::abort("Argument `length_out` is missing with no default")
}

# If idx is length 1 explicitly pass frequency
if (length(idx) == 1) {
frequency <- 1/4
} else {
frequency <- NULL
}

ret <- make_sequential_timeseries_regular_freq(
idx = idx,
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future)
n_future = n_future,
frequency = frequency)

return(ret)
}
Expand All @@ -436,12 +470,20 @@ tk_make_future_timeseries.numeric <- function(idx, length_out, inspect_weekdays
rlang::abort("Argument `length_out` is missing with no default")
}

# If idx is length 1 explicitly pass frequency
if (length(idx) == 1) {
frequency <- 1
} else {
frequency <- NULL
}

ret <- make_sequential_timeseries_regular_freq(
idx = idx,
length_out = length_out,
skip_values = skip_values,
insert_values = insert_values,
n_future = n_future)
n_future = n_future,
frequency = frequency)

return(ret)
}
Expand Down Expand Up @@ -572,7 +614,7 @@ predict_future_timeseries_daily <- function(idx, length_out, inspect_weekdays, i
return(ret)
}

make_sequential_timeseries_irregular_freq <- function(idx, length_out, skip_values, insert_values, n_future) {
make_sequential_timeseries_irregular_freq <- function(idx, length_out, skip_values, insert_values, n_future, frequency = NULL) {

# n_future will be TRUE/FALSE (and length_out = n_future)
# - If TRUE, returns the old n_future behavior of number of observations being potentially fewer than n_future
Expand Down Expand Up @@ -605,7 +647,9 @@ make_sequential_timeseries_irregular_freq <- function(idx, length_out, skip_valu

# Create date sequence based on index.num and median frequency
last_numeric_date <- dplyr::last(idx_signature$index.num)
frequency <- idx_summary$diff.median
if (is.null(frequency)) {
frequency <- idx_summary$diff.median
}
next_numeric_date <- last_numeric_date + frequency

if (is.null(n_future)) n_future <- FALSE
Expand Down Expand Up @@ -661,7 +705,7 @@ make_sequential_timeseries_irregular_freq <- function(idx, length_out, skip_valu
}


make_sequential_timeseries_regular_freq <- function(idx, length_out, skip_values, insert_values, n_future) {
make_sequential_timeseries_regular_freq <- function(idx, length_out, skip_values, insert_values, n_future, frequency = NULL) {

# n_future will be TRUE/FALSE (and length_out = n_future)
# - If TRUE, returns the old n_future behavior of number of observations being potentially fewer than n_future
Expand Down Expand Up @@ -697,7 +741,9 @@ make_sequential_timeseries_regular_freq <- function(idx, length_out, skip_values

# Create date sequence based on index.num and median frequency
last_numeric_date <- dplyr::last(idx_numeric)
frequency <- median_diff
if (is.null(frequency)) {
frequency <- median_diff
}
next_numeric_date <- last_numeric_date + frequency

# print(list(
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-tk_make_timeseries_future.R
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,23 @@ test_that("tk_make_future_timeseries(): End of Month", {

})

test_that("tk_make_future_timeseries() handles length 1 inputs", {

idx_qtr <- zoo::as.yearqtr("2023-01")
expect_qtr <- zoo::as.yearqtr("2023-02")
expect_equal(tk_make_future_timeseries(idx_qtr, 1), expect_qtr)

idx_posixt <- as.POSIXct("2023-01-01 00:00:00")
expect_posixt <- as.POSIXct("2023-01-01 00:00:01")
expect_equal(tk_make_future_timeseries(idx_posixt, 1), expect_posixt)

idx_yearmon <- zoo::as.yearmon("2023-01")
expect_yearmon <- zoo::as.yearmon("2023-02")
expect_equal(tk_make_future_timeseries(idx_yearmon, 1), expect_yearmon)

idx_date <- as.Date("2023-01-01")
expect_date <- as.Date("2023-01-02")
expect_equal(tk_make_future_timeseries(idx_date, 1), expect_date)

})