Skip to content

Commit 009ac43

Browse files
authored
Merge pull request #441 from cmu-delphi/lcb/guess-time-type-not-weeks-as-years
Guess YYYYww numeric as "custom", fix incidental typo
2 parents eec777f + 248a0e8 commit 009ac43

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: epiprocess
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.7.7
4+
Version: 0.7.8
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
2727
the `epi_df` (#382).
2828
- Refactored internals to use `cli` for warnings/errors and `checkmate` for
2929
argument checking (#413).
30-
- Fix logic to auto-assign `ep_df` `time_type` to `week` (#416).
30+
- Fix logic to auto-assign `epi_df` `time_type` to `week` (#416) and `year`
31+
(#441).
3132

3233
## Breaking changes
3334

R/utils.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ guess_geo_type <- function(geo_value) {
402402
guess_time_type <- function(time_value) {
403403
# Convert character time values to Date or POSIXct
404404
if (is.character(time_value)) {
405-
if (nchar(time_value[1]) <= "10") {
405+
if (nchar(time_value[1]) <= 10L) {
406406
new_time_value <- tryCatch(
407407
{
408408
as.Date(time_value)
@@ -439,14 +439,8 @@ guess_time_type <- function(time_value) {
439439
return("yearmonth")
440440
} else if (inherits(time_value, "yearquarter")) {
441441
return("yearquarter")
442-
}
443-
444-
# Else, if it's an integer that's at least 1582, then use "year"
445-
if (
446-
is.numeric(time_value) &&
447-
all(time_value == as.integer(time_value)) &&
448-
all(time_value >= 1582)
449-
) {
442+
} else if (rlang::is_integerish(time_value) &&
443+
all(nchar(as.character(time_value)) == 4L)) { # nolint: indentation_linter
450444
return("year")
451445
}
452446

tests/testthat/test-utils.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ test_that("guess_time_type works for different types", {
4646
yearquarters <- tsibble::yearquarter(10)
4747

4848
years <- c(1999, 2000)
49+
ambiguous_yearweeks <- c(199901, 199902) # -> "custom"
50+
51+
daytimes <- as.POSIXct(c("2022-01-01 05:00:00", "2022-01-01 15:0:00"), tz = "UTC")
52+
daytimes_chr <- as.character(daytimes)
4953

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

6468
expect_equal(guess_time_type(years), "year")
69+
expect_equal(guess_time_type(ambiguous_yearweeks), "custom")
70+
71+
expect_equal(guess_time_type(daytimes), "day-time")
72+
expect_equal(guess_time_type(daytimes_chr), "day-time")
6573

6674
expect_equal(guess_time_type(not_ymd1), "custom")
6775
expect_equal(guess_time_type(not_ymd2), "custom")
6876
expect_equal(guess_time_type(not_ymd3), "custom")
6977
expect_equal(guess_time_type(not_a_date), "custom")
7078
})
71-
3
79+
7280
test_that("guess_time_type works with gaps", {
7381
days_gaps <- as.Date("2022-01-01") + c(0, 1, 3, 4, 8, 8 + 7)
7482
weeks_gaps <- as.Date("2022-01-01") + 7 * c(0, 1, 3, 4, 8, 8 + 7)

0 commit comments

Comments
 (0)