From 2814c811df4d3b4cfa10eab55f80e7d49c03d578 Mon Sep 17 00:00:00 2001 From: Jan Gorecki Date: Tue, 14 Aug 2018 06:37:32 +0530 Subject: [PATCH] proper NA handling in as.ITime.character, closes #2940 (#2941) --- NEWS.md | 2 ++ R/IDateTime.R | 2 +- inst/tests/tests.Rraw | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 3f1fa6e33..bc33f2153 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,8 @@ 2. `gmin` and `gmax` no longer fail on _ordered_ factors, [#1947](https://github.com/Rdatatable/data.table/issues/1947). Thanks to @mcieslik-mctp for identifying and @mbacou for the nudge. +3. `as.ITime.character` now properly handles NA when attempting to detect the format of non-NA values in vector. Thanks @polyjian for reporting, closes [#2940](https://github.com/Rdatatable/data.table/issues/2940). + #### NOTES 1. The type coercion warning message has been improved, [#2989](https://github.com/Rdatatable/data.table/pull/2989). Thanks to @sarahbeeysian on [Twitter](https://twitter.com/sarahbeeysian/status/1021359529789775872) for highlighting. For example, given the follow statements: diff --git a/R/IDateTime.R b/R/IDateTime.R index 810b3d11c..051dede10 100644 --- a/R/IDateTime.R +++ b/R/IDateTime.R @@ -133,7 +133,7 @@ as.ITime.character <- function(x, format, ...) { new <- strptime(x[w], format = f, ...) nna <- !is.na(new) if (any(nna)) { - y[ w[nna] ] <- new + y[ w[nna] ] <- new[nna] w <- w[!nna] } } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d7bd36452..c4b2f4dd9 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -12020,6 +12020,9 @@ test(1918.5, DT[, min(V1), by=V2], data.table(V2=c("f", "g", "h"), V1=structure( test(1918.6, DT[, max(V1), by=V2], data.table(V2=c("f", "g", "h"), V1=structure(3:5, .Label=lev, class=c("ordered", "factor")))) options(old) +# as.ITime.character bug for NA handling #2940 +test(1919, as.ITime(c('xxx', '10:43')), structure(c(NA, 38580L), class = "ITime")) + ################################### # Add new tests above this line # ###################################