Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for entirely missing columns in guess_dates #109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 8 additions & 1 deletion R/guess_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-guess_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down