diff --git a/R/as-tsibble.R b/R/as-tsibble.R index 0a5178d3..ca8f649c 100644 --- a/R/as-tsibble.R +++ b/R/as-tsibble.R @@ -498,7 +498,7 @@ duplicated_key_index <- function(data, key, index) { # time zone associated with the index will be dropped, # e.g. nycflights13::weather, thus result in duplicates. # dup <- anyDuplicated(data[, identifiers, drop = FALSE]) - res <- grouped_df(data, vars = key) %>% + res <- grouped_df(as_tibble(data), key) %>% summarise(!! "zzz" := anyDuplicated.default(!! index)) %>% dplyr::pull(!! "zzz") any_not_equal_to_c(res, 0) diff --git a/R/index-by.R b/R/index-by.R index 122f6f32..c04ddc66 100644 --- a/R/index-by.R +++ b/R/index-by.R @@ -78,11 +78,10 @@ index_by.tbl_ts <- function(.data, ...) { if (identical(idx_chr, expr_name)) { abort(sprintf("Column `%s` (index) can't be overwritten.", idx_chr)) } + idx2 <- sym(expr_name) # ungroup() protect the index class tbl <- mutate(ungroup(.data), !!! exprs) %>% - group_by(!!! groups(.data)) %>% - new_tsibble_class("grouped_ts") - idx2 <- sym(expr_name) + group_by(!!! groups(.data), !! idx2) build_tsibble( tbl, key = key(.data), index = !! idx, index2 = !! idx2, regular = is_regular(.data), validate = FALSE, diff --git a/R/key-group.R b/R/key-group.R index 1b6f941a..ba65faec 100644 --- a/R/key-group.R +++ b/R/key-group.R @@ -56,11 +56,12 @@ key_by.tbl_ts <- function(.data, ...) { exprs <- enexprs(...) key <- validate_key(.data, exprs) validate <- !all(is.element(key(.data), key)) + idx <- index(.data) if (validate) { - .data <- retain_tsibble(.data, key, index(.data)) + .data <- retain_tsibble(.data, key, idx) } build_tsibble_meta( - .data, key = key, index = !! index(.data), index2 = !! index2(.data), + .data, key = key, index = !! idx, index2 = !! index2(.data), regular = is_regular(.data), ordered = is_ordered(.data), interval = interval(.data) ) @@ -118,7 +119,7 @@ key_indices <- function(x) { #' @export key_indices.tbl_ts <- function(x) { key_vars <- key_vars(x) - grped_key <- grouped_df(x, key_vars) + grped_key <- grouped_df(as_tibble(x), key_vars) group_indices(grped_key) } diff --git a/tests/testthat/test-tsibble.R b/tests/testthat/test-tsibble.R index 52817cfa..e05c5490 100644 --- a/tests/testthat/test-tsibble.R +++ b/tests/testthat/test-tsibble.R @@ -64,7 +64,7 @@ test_that("nanotime with 1 nanoseconds interval", { test_that("POSIXt with 1 second interval", { expect_identical(index_valid(dat_x$date_time), TRUE) - expect_message(tsbl <- as_tsibble(dat_x)) + expect_message(tsbl <- as_tsibble(dat_x), "Using `date_time` as index variable.") expect_output(print(tsbl), "A tsibble: 5 x 2 \\[1s\\]") expect_error(as_tsibble(dat_x, key = id(date_time))) expect_is(tsbl, "tbl_ts")