Skip to content

Commit

Permalink
time() -> time_ts() (no export)
Browse files Browse the repository at this point in the history
closes #277
  • Loading branch information
earowang committed Oct 9, 2022
1 parent 5f26be3 commit 189d7a0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v1
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: r-lib/pkgdown
extra-packages: any::pkgdown, local::.
needs: website

- name: Deploy package
Expand Down
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ S3method(summarise,grouped_ts)
S3method(summarise,tbl_ts)
S3method(tbl_sum,grouped_ts)
S3method(tbl_sum,tbl_ts)
S3method(time,Date)
S3method(time,POSIXt)
S3method(time,numeric)
S3method(time,yearmonth)
S3method(time,yearquarter)
S3method(time,yearweek)
S3method(transmute,grouped_ts)
S3method(transmute,tbl_ts)
S3method(type_sum,tbl_ts)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Fixed the argument of `.full` in `*_gaps()` when inputs like `T`, `F` are valid. (#275)
* Fixed validation of `index2` in `build_tsibble()` when a column name contains "index". (#284)
* No longer export `time()` methods (renamed to `time_ts()`) to contaminate `time()` in `stats::window()`. (#277)

# tsibble 1.1.2

Expand Down
68 changes: 38 additions & 30 deletions R/filter-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,39 @@ time_in <- function(x, ...) {
f <- new_formula(f, f)
}
env <- f_env(f)
lhs[[i]] <- start(x, eval_bare(is_dot_null(f_lhs(f)), env = env))
rhs[[i]] <- end(x, eval_bare(is_dot_null(f_rhs(f)), env = env))
lhs[[i]] <- start_window(x, eval_bare(is_dot_null(f_lhs(f)), env = env))
rhs[[i]] <- end_window(x, eval_bare(is_dot_null(f_rhs(f)), env = env))
lgl[[i]] <- eval_bare(x >= lhs[[i]] & x < rhs[[i]])
}

reduce(lgl, `|`)
}

start.numeric <- function(x, y = NULL, ...) {
start_window <- function(x, y = NULL, ...) {
UseMethod("start_window")
}

end_window <- function(x, y = NULL, ...) {
UseMethod("end_window")
}

start_window.numeric <- function(x, y = NULL, ...) {
if (is_null(y)) {
min(x)
} else {
as.numeric(y)
}
}

end.numeric <- function(x, y = NULL, ...) {
end_window.numeric <- function(x, y = NULL, ...) {
if (is_null(y)) {
max(x) + 1
} else {
as.numeric(y) + 1
}
}

start.difftime <- function(x, y = NULL, ...) {
start_window.difftime <- function(x, y = NULL, ...) {
if (!requireNamespace("hms", quietly = TRUE)) {
abort("Package `hms` required.\nPlease install and try again.")
}
Expand All @@ -137,7 +145,7 @@ start.difftime <- function(x, y = NULL, ...) {
}
}

end.difftime <- function(x, y = NULL, ...) {
end_window.difftime <- function(x, y = NULL, ...) {
if (is_null(y)) {
hms::as_hms(max(x) + 1)
} else {
Expand All @@ -147,7 +155,7 @@ end.difftime <- function(x, y = NULL, ...) {
}
}

start.Date <- function(x, y = NULL, ...) {
start_window.Date <- function(x, y = NULL, ...) {
if (is_null(y)) {
min(x)
} else {
Expand All @@ -159,7 +167,7 @@ start.Date <- function(x, y = NULL, ...) {
}
}

end.Date <- function(x, y = NULL, ...) {
end_window.Date <- function(x, y = NULL, ...) {
if (is_null(y)) {
max(x) + period(1, "day")
} else {
Expand All @@ -185,7 +193,7 @@ end.Date <- function(x, y = NULL, ...) {
}
}

start.POSIXct <- function(x, y = NULL, ...) {
start_window.POSIXct <- function(x, y = NULL, ...) {
if (is_null(y)) {
min(x)
} else {
Expand All @@ -196,7 +204,7 @@ start.POSIXct <- function(x, y = NULL, ...) {
}
}

end.POSIXct <- function(x, y = NULL, ...) {
end_window.POSIXct <- function(x, y = NULL, ...) {
if (is_null(y)) {
max(x) + period(1, "second")
} else {
Expand Down Expand Up @@ -226,80 +234,80 @@ end.POSIXct <- function(x, y = NULL, ...) {
}
}

start.yearweek <- function(x, y = NULL, ...) {
start_window.yearweek <- function(x, y = NULL, ...) {
wk_start <- week_start(x)
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearweek")
y <- as.character(as_date(yearweek(y, week_start = wk_start)))
}
yearweek(start(x = x, y = y), week_start = wk_start)
yearweek(start_window(x = x, y = y), week_start = wk_start)
}

end.yearweek <- function(x, y = NULL, ...) {
end_window.yearweek <- function(x, y = NULL, ...) {
wk_start <- week_start(x)
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearweek")
y <- as.character(as_date(yearweek(y, week_start = wk_start)))
}
yearweek(end(x = x, y = y), week_start = wk_start) + 1
yearweek(end_window(x = x, y = y), week_start = wk_start) + 1
}

start.yearmonth <- function(x, y = NULL, ...) {
start_window.yearmonth <- function(x, y = NULL, ...) {
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearmonth")
y <- as.character(as_date(yearmonth(y)))
}
yearmonth(start(x = x, y = y))
yearmonth(start_window(x = x, y = y))
}

end.yearmonth <- function(x, y = NULL, ...) {
end_window.yearmonth <- function(x, y = NULL, ...) {
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearmonth")
y <- as.character(as_date(yearmonth(y)))
}
yearmonth(end(x = x, y = y)) + 1
yearmonth(end_window(x = x, y = y)) + 1
}

start.yearquarter <- function(x, y = NULL, ...) {
start_window.yearquarter <- function(x, y = NULL, ...) {
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearquarter")
y <- as.character(as_date(yearquarter(y)))
}
yearquarter(start(x = x, y = y))
yearquarter(start_window(x = x, y = y))
}

end.yearquarter <- function(x, y = NULL, ...) {
end_window.yearquarter <- function(x, y = NULL, ...) {
x <- as_date(x)
if (!is_null(y)) {
abort_not_chr(y, class = "yearquarter")
y <- as.character(as_date(yearquarter(y)))
}
yearquarter(end(x = x, y = y)) + 1
yearquarter(end_window(x = x, y = y)) + 1
}

start.yearmon <- function(x, y = NULL, ...) {
start_window.yearmon <- function(x, y = NULL, ...) {
x <- yearmonth(x)
start(x, y = y)
start_window(x, y = y)
}

end.yearmon <- function(x, y = NULL, ...) {
end_window.yearmon <- function(x, y = NULL, ...) {
x <- yearmonth(x)
end(x, y = y)
end_window(x, y = y)
}

start.yearqtr <- function(x, y = NULL, ...) {
start_window.yearqtr <- function(x, y = NULL, ...) {
x <- yearquarter(x)
start(x, y = y)
start_window(x, y = y)
}

end.yearqtr <- function(x, y = NULL, ...) {
end_window.yearqtr <- function(x, y = NULL, ...) {
x <- yearquarter(x)
end(x, y = y)
end_window(x, y = y)
}

is_dot_null <- function(x) { # x is a sym
Expand Down
24 changes: 11 additions & 13 deletions R/tsibble2ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ as.ts.tbl_ts <- function(x, value, frequency = NULL, fill = NA_real_, ...) {
pivot_wider_ts <- function(data, frequency = NULL) {
index <- index_var(data)
df_rows <- data[[index]]
idx_time <- time(df_rows)
idx_time <- time_ts(df_rows)
rows <- vec_unique(df_rows)
key_rows <- key_rows(data)
mvars <- measured_vars(data)
Expand All @@ -69,43 +69,41 @@ pivot_wider_ts <- function(data, frequency = NULL) {
ts(res, start(idx_time), frequency = frequency)
}

#' @export
time.yearweek <- function(x, ...) {
time_ts <- function(x, ...) {
UseMethod("time_ts")
}

time_ts.yearweek <- function(x, ...) {
freq <- guess_frequency(x)
y <- decimal_date(x)
ts(y, start = min0(y), frequency = freq)
}

#' @export
time.yearmonth <- function(x, ...) {
time_ts.yearmonth <- function(x, ...) {
freq <- guess_frequency(x)
y <- year(x) + (month(x) - 1) / freq
ts(y, start = min0(y), frequency = freq)
}

#' @export
time.yearquarter <- function(x, ...) {
time_ts.yearquarter <- function(x, ...) {
freq <- guess_frequency(x)
y <- year(x) + (quarter(x) - 1) / freq
ts(y, start = min0(y), frequency = freq)
}

#' @export
time.numeric <- function(x, ...) {
time_ts.numeric <- function(x, ...) {
ts(x, start = min0(x), frequency = 1)
}

#' @export
time.Date <- function(x, frequency = NULL, ...) {
time_ts.Date <- function(x, frequency = NULL, ...) {
if (is.null(frequency)) {
frequency <- guess_frequency(x)
}
y <- decimal_date(x)
ts(x, start = min0(y), frequency = frequency)
}

#' @export
time.POSIXt <- function(x, frequency = NULL, ...) {
time_ts.POSIXt <- function(x, frequency = NULL, ...) {
if (is.null(frequency)) {
frequency <- guess_frequency(x)
}
Expand Down
42 changes: 21 additions & 21 deletions tests/testthat/test-filter-index.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
x <- c("2016", "2016-10", "2016-12-09")

test_that("class: Date", {
expect_error(start(pedestrian$Date, 2017), "Must be")
expect_equal(start(pedestrian$Date), as.Date("2015-01-01"))
expect_error(start_window(pedestrian$Date, 2017), "Must be")
expect_equal(start_window(pedestrian$Date), as.Date("2015-01-01"))
expect_equal(
start(pedestrian$Date, x),
start_window(pedestrian$Date, x),
as.Date(c("2016-01-01", "2016-10-01", "2016-12-09"))
)
expect_equal(end(pedestrian$Date), as.Date("2017-01-01"))
expect_equal(end_window(pedestrian$Date), as.Date("2017-01-01"))
expect_equal(
end(pedestrian$Date, x),
end_window(pedestrian$Date, x),
as.Date(c("2017-01-01", "2016-11-01", "2016-12-10"))
)
})

test_that("class: year*", {
yrwk <- new_yearweek(unique(pedestrian$Date))
expect_equal(start(yrwk), yearweek(as.Date("2015-01-01")))
expect_equal(start_window(yrwk), yearweek(as.Date("2015-01-01")))
expect_equal(
start(yrwk, x),
start_window(yrwk, x),
yearweek(as.Date(c("2015-12-28", "2016-09-26", "2016-12-05")))
)
expect_equal(end(pedestrian$Date), as.Date("2017-01-01"))
expect_equal(end_window(pedestrian$Date), as.Date("2017-01-01"))
expect_equal(
end(pedestrian$Date, x),
end_window(pedestrian$Date, x),
as.Date(c("2017-01-01", "2016-11-01", "2016-12-10"))
)
x <- yearquarter(c("2013 Q3", "2013 Qtr 3", "Quarter 4 2015"))
Expand All @@ -33,7 +33,7 @@ test_that("class: year*", {

test_that("class: yearweek with no-default week start #261", {
yrwk <- new_yearweek(unique(pedestrian$Date), week_start = 7)
expect_equal(start(yrwk), yearweek(as.Date("2015-01-01"), week_start = 7))
expect_equal(start_window(yrwk), yearweek(as.Date("2015-01-01"), week_start = 7))
})

si <- sessionInfo()
Expand All @@ -45,18 +45,18 @@ tz <- "Australia/Melbourne"

test_that("class: POSIXct", {
skip_on_cran()
expect_error(start(pedestrian$Date_Time, 2017), "Must be")
expect_equal(start(pedestrian$Date_Time), ymd("2015-01-01", tz = tz))
expect_error(start_window(pedestrian$Date_Time, 2017), "Must be")
expect_equal(start_window(pedestrian$Date_Time), ymd("2015-01-01", tz = tz))
expect_equal(
start(pedestrian$Date_Time, y),
start_window(pedestrian$Date_Time, y),
ymd_hm(
c("2016-01-01 00:00", "2016-10-01 00:00", "2016-12-09 00:00", "2016-12-09 10:00"),
tz = tz
)
)
expect_equal(end(pedestrian$Date_Time), ymd_hms("2016-12-31 23:00:01", tz = tz))
expect_equal(end_window(pedestrian$Date_Time), ymd_hms("2016-12-31 23:00:01", tz = tz))
expect_equal(
end(pedestrian$Date_Time, y),
end_window(pedestrian$Date_Time, y),
ymd_hms(
c("2017-01-01 00:00:00", "2016-11-01 00:00:00", "2016-12-10 00:00:00", "2016-12-09 10:00:01"),
tz = tz
Expand All @@ -66,12 +66,12 @@ test_that("class: POSIXct", {

test_that("class: hms", {
z <- hms::as_hms(unique(pedestrian$Date_Time))
expect_error(start(z, y = 11), "Must be")
expect_equal(start(z), hms::as_hms("00:00:00"))
# expect_error(start(z, y = "11:00"), "cannot be expressed as difftime")
expect_equal(start(z, y = "11:00:00"), hms::as_hms("11:00:00"))
expect_equal(end(z), hms::as_hms("23:00:01"))
expect_equal(end(z, y = "21:00:00"), hms::as_hms("21:00:01"))
expect_error(start_window(z, y = 11), "Must be")
expect_equal(start_window(z), hms::as_hms("00:00:00"))
# expect_error(start_window(z, y = "11:00"), "cannot be expressed as difftime")
expect_equal(start_window(z, y = "11:00:00"), hms::as_hms("11:00:00"))
expect_equal(end_window(z), hms::as_hms("23:00:01"))
expect_equal(end_window(z, y = "21:00:00"), hms::as_hms("21:00:01"))
})

test_that("filter_index()", {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-tsibble2ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ test_that("as.ts.tbl_ts(fill = )", {

test_that("time.* and guess_frequency.*", {
dat <- seq(as.Date("2017-01-01"), as.Date("2017-01-31"), by = 1)
y <- time(dat)
expect_s3_class(y, "ts")
y <- time_ts(dat)
expect_equal(frequency(y), 7)
expect_equal(guess_frequency(dat), 7)
dat_min <- seq(
Expand Down

0 comments on commit 189d7a0

Please sign in to comment.