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

Add mday to features argument in step_date() #1360

Merged
merged 4 commits into from
Aug 15, 2024
Merged
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
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
Loading