Skip to content

Commit

Permalink
Merge pull request #1360 from Edgar-Zamora/add-mday-stepdate
Browse files Browse the repository at this point in the history
Add mday to `features` argument in `step_date()`
  • Loading branch information
EmilHvitfeldt authored Aug 15, 2024
2 parents 63628f5 + eb9c9b7 commit 38a988d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ importFrom(lubridate,am)
importFrom(lubridate,decimal_date)
importFrom(lubridate,hour)
importFrom(lubridate,is.Date)
importFrom(lubridate,mday)
importFrom(lubridate,minute)
importFrom(lubridate,month)
importFrom(lubridate,quarter)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

* `step_dummy()` now throws more informative warnings for `NA` values. (#450)

* `step_date()` now accepts `"mday"` as a possible feature. (@Edgar-Zamora, #1211)

## Bug Fixes

* `NA` levels in factors aren't dropped when passed to `recipe()`. (#1291)
Expand Down
6 changes: 5 additions & 1 deletion R/date.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' for this step. The selected variables should have class `Date` or
#' `POSIXct`. See [selections()] for more details.
#' @param features A character string that includes at least one
#' of the following values: `month`, `dow` (day of week),
#' of the following values: `month`, `dow` (day of week), `mday` (day of month),
#' `doy` (day of year), `week`, `month`,
#' `decimal` (decimal date, e.g. 2002.197), `quarter`,
#' `semester`, `year`.
Expand Down Expand Up @@ -98,6 +98,7 @@ step_date <-
c(
"year",
"doy",
"mday",
"week",
"decimal",
"semester",
Expand Down Expand Up @@ -202,6 +203,9 @@ get_date_features <-
if ("doy" %in% feats) {
res[, grepl("doy$", names(res))] <- vec_cast(yday(dt), integer())
}
if ("mday" %in% feats) {
res[, grepl("mday$", names(res))] <- vec_cast(mday(dt), integer())
}
if ("week" %in% feats) {
res[, grepl("week$", names(res))] <- vec_cast(week(dt), integer())
}
Expand Down
1 change: 1 addition & 0 deletions R/recipes-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#' @importFrom lubridate wday
#' @importFrom lubridate week
#' @importFrom lubridate yday
#' @importFrom lubridate mday
#' @importFrom lubridate year
#' @importFrom Matrix Matrix
#' @importFrom purrr map
Expand Down
7 changes: 4 additions & 3 deletions man/roles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/step_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/test-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test_that("default option", {

test_that("nondefault options", {
date_rec <- recipe(~ Dan + Stefan, examples) %>%
step_date(all_predictors(), features = c("dow", "month"), label = FALSE)
step_date(all_predictors(), features = c("dow", "month", "mday"), label = FALSE)

date_rec <- prep(date_rec, training = examples)
date_res <- bake(date_rec, new_data = examples)
Expand All @@ -61,8 +61,10 @@ test_that("nondefault options", {
Stefan = examples$Stefan,
Dan_dow = wday(examples$Dan, label = FALSE),
Dan_month = month(examples$Dan, label = FALSE),
Dan_mday = mday(examples$Dan),
Stefan_dow = wday(examples$Stefan, label = FALSE),
Stefan_month = month(examples$Stefan, label = FALSE)
Stefan_month = month(examples$Stefan, label = FALSE),
Stefan_mday = mday(examples$Stefan)
)

expect_equal(date_res, date_exp)
Expand Down

0 comments on commit 38a988d

Please sign in to comment.