Skip to content

Commit

Permalink
feat(task): allow date feature (#1237)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke authored Jan 6, 2025
1 parent 52deba9 commit 47a3a51
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
29 changes: 29 additions & 0 deletions R/auto_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ ee[["logical___POSIXct"]] =
function(value, type, levels) {
if (allMissing(value)) .POSIXct(value, tz = "") else value
}
ee[["logical___Date"]] =
function(value, type, levels) {
if (allMissing(value)) as.Date(value) else value
}

## from: integer
ee[["integer___logical"]] =
Expand All @@ -44,6 +48,8 @@ ee[["integer___ordered"]] =
ee[["logical___ordered"]]
ee[["integer___POSIXct"]] =
ee[["logical___POSIXct"]]
ee[["integer___Date"]] =
ee[["logical___Date"]]

## from: numeric
ee[["numeric___logical"]] =
Expand All @@ -60,6 +66,8 @@ ee[["numeric___ordered"]] =
ee[["logical___ordered"]]
ee[["numeric___POSIXct"]] =
ee[["logical___POSIXct"]]
ee[["numeric___Date"]] =
ee[["logical___Date"]]

## from: character
ee[["character___logical"]] =
Expand All @@ -83,6 +91,11 @@ ee[["character___POSIXct"]] =
x = try(as.POSIXct(value, ""), silent = TRUE)
if (inherits(x, "try-error")) value else x
}
ee[["character___Date"]] =
function(value, type, levels) {
x = try(as.Date(value), silent = TRUE)
if (inherits(x, "try-error")) value else x
}

## from: factor
ee[["factor___logical"]] =
Expand All @@ -99,6 +112,8 @@ ee[["factor___ordered"]] =
}
ee[["factor___POSIXct"]] =
ee[["character___POSIXct"]]
ee[["factor___Date"]] =
ee[["character___Date"]]

## from: ordered
ee[["ordered___character"]] =
Expand All @@ -109,6 +124,20 @@ ee[["ordered___ordered"]] =
ee[["ordered___ordered"]]
ee[["ordered___POSIXct"]] =
ee[["character___POSIXct"]]
ee[["ordered___Date"]] =
ee[["character___Date"]]

## from: POSIXct
ee[["POSIXct___Date"]] =
function(value, type, levels) {
as.Date(value)
}

## from: Date
ee[["Date___POSIXct"]] =
function(value, type, levels) {
as.POSIXct(value)
}

rm(ee)
# nolint end
Expand Down
2 changes: 1 addition & 1 deletion R/mlr_reflections.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ local({
)

mlr_reflections$task_feature_types = c(
lgl = "logical", int = "integer", dbl = "numeric", chr = "character", fct = "factor", ord = "ordered", pxc = "POSIXct"
lgl = "logical", int = "integer", dbl = "numeric", chr = "character", fct = "factor", ord = "ordered", pxc = "POSIXct", dte = "Date"
)

mlr_reflections$task_row_roles = c(
Expand Down
3 changes: 2 additions & 1 deletion inst/testthat/helper_autotest.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ generate_data = function(learner, N) {
character = sample(rep_len(letters[1:2], N)),
factor = sample(factor(rep_len(c("f1", "f2"), N), levels = c("f1", "f2"))),
ordered = sample(ordered(rep_len(c("o1", "o2"), N), levels = c("o1", "o2"))),
POSIXct = Sys.time() - runif(N, min = 0, max = 10 * 365 * 24 * 60 * 60)
POSIXct = Sys.time() - runif(N, min = 0, max = 10 * 365 * 24 * 60 * 60),
Date = Sys.Date() - runif(N, min = 0, max = 10 * 365)
)
}
types = unique(learner$feature_types)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test_auto_convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,10 @@ test_that("POSIXct", {
auto_convert("2020-01-20 10:00:00", "x", "POSIXct", character()),
as.POSIXct("2020-01-20 10:00:00", "")
)

expect_date(auto_convert(Sys.time(), "x", "Date", character()))
})

test_that("Date", {
expect_date(auto_convert("2021-04-21", "x", "Date", character()))
})

0 comments on commit 47a3a51

Please sign in to comment.