diff --git a/NEWS.md b/NEWS.md index 4e7fc7f..a2f8efa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,10 +1,13 @@ # linelist 0.0.40.9000 + * Both `clean_variable_spelling()` and `clean_spelling()` have been migrated over to the {matchmaker} package and arguments from the aformentioned functions are passed to the {matchmaker} functions. Tests and documentation have been updated to reflect this. * Remove {rlang} from imports (but is still imported by {matchmaker}). +* `guess_dates()` will now return entirely missing vectors unchanged instead of + throwing an error. This fixes #108 via #109. # linelist 0.0.39.9000 diff --git a/R/guess_dates.R b/R/guess_dates.R index a557b6c..092e76c 100644 --- a/R/guess_dates.R +++ b/R/guess_dates.R @@ -205,14 +205,21 @@ guess_dates <- function(x, error_tolerance = 0.1, first_date = NULL, # save the original x for later if nothing is converted ox <- x + if (all(is.na(x))) { + warning("all dates were missing, returning data unchanged", call. = FALSE) + return(ox) + } + if (is.factor(x)) { x <- as.character(x) } - + if (!is.character(x)) { stop("guess dates will only work for characters and factors") } + + # Process lubridate order list ----------------------------------------------- if (!is.list(orders) && is.character(orders)) { diff --git a/tests/testthat/test-guess_dates.R b/tests/testthat/test-guess_dates.R index e4a5995..0f4690d 100644 --- a/tests/testthat/test-guess_dates.R +++ b/tests/testthat/test-guess_dates.R @@ -10,11 +10,19 @@ expected_result <- structure(c(4417, 17793, 11323, 15321, 15647, 16052, test_that("only characters and factors are expected", { - expect_error(guess_dates(NULL), "guess dates will only work for characters and factors") expect_error(guess_dates(pi), "guess dates will only work for characters and factors") }) +test_that("vectors of all missing values are returned unharmed", { + na <- factor(c(NA, NA)) + expect_warning({ + expect_identical(guess_dates(na), na) + }, "all dates were missing, returning data unchanged") + expect_warning({ + expect_identical(guess_dates(NULL), NULL) + }, "all dates were missing, returning data unchanged") +}) test_that("mixed formats work", { expect_equal(guess_dates(x, error_tolerance = 0.8, first_date = as.Date("1980-01-01")),