Skip to content

Commit

Permalink
Merge pull request #441 from cmu-delphi/lcb/guess-time-type-not-weeks…
Browse files Browse the repository at this point in the history
…-as-years

Guess YYYYww numeric as "custom", fix incidental typo
  • Loading branch information
brookslogan authored May 3, 2024
2 parents eec777f + 248a0e8 commit 009ac43
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: epiprocess
Title: Tools for basic signal processing in epidemiology
Version: 0.7.7
Version: 0.7.8
Authors@R: c(
person("Jacob", "Bien", role = "ctb"),
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
the `epi_df` (#382).
- Refactored internals to use `cli` for warnings/errors and `checkmate` for
argument checking (#413).
- Fix logic to auto-assign `ep_df` `time_type` to `week` (#416).
- Fix logic to auto-assign `epi_df` `time_type` to `week` (#416) and `year`
(#441).

## Breaking changes

Expand Down
12 changes: 3 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ guess_geo_type <- function(geo_value) {
guess_time_type <- function(time_value) {
# Convert character time values to Date or POSIXct
if (is.character(time_value)) {
if (nchar(time_value[1]) <= "10") {
if (nchar(time_value[1]) <= 10L) {
new_time_value <- tryCatch(
{
as.Date(time_value)
Expand Down Expand Up @@ -439,14 +439,8 @@ guess_time_type <- function(time_value) {
return("yearmonth")
} else if (inherits(time_value, "yearquarter")) {
return("yearquarter")
}

# Else, if it's an integer that's at least 1582, then use "year"
if (
is.numeric(time_value) &&
all(time_value == as.integer(time_value)) &&
all(time_value >= 1582)
) {
} else if (rlang::is_integerish(time_value) &&
all(nchar(as.character(time_value)) == 4L)) { # nolint: indentation_linter
return("year")
}

Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ test_that("guess_time_type works for different types", {
yearquarters <- tsibble::yearquarter(10)

years <- c(1999, 2000)
ambiguous_yearweeks <- c(199901, 199902) # -> "custom"

daytimes <- as.POSIXct(c("2022-01-01 05:00:00", "2022-01-01 15:0:00"), tz = "UTC")
daytimes_chr <- as.character(daytimes)

# YYYY-MM-DD is the accepted format
not_ymd1 <- "January 1, 2022"
Expand All @@ -62,13 +66,17 @@ test_that("guess_time_type works for different types", {
expect_equal(guess_time_type(yearquarters), "yearquarter")

expect_equal(guess_time_type(years), "year")
expect_equal(guess_time_type(ambiguous_yearweeks), "custom")

expect_equal(guess_time_type(daytimes), "day-time")
expect_equal(guess_time_type(daytimes_chr), "day-time")

expect_equal(guess_time_type(not_ymd1), "custom")
expect_equal(guess_time_type(not_ymd2), "custom")
expect_equal(guess_time_type(not_ymd3), "custom")
expect_equal(guess_time_type(not_a_date), "custom")
})
3

test_that("guess_time_type works with gaps", {
days_gaps <- as.Date("2022-01-01") + c(0, 1, 3, 4, 8, 8 + 7)
weeks_gaps <- as.Date("2022-01-01") + 7 * c(0, 1, 3, 4, 8, 8 + 7)
Expand Down

0 comments on commit 009ac43

Please sign in to comment.