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

[ENH] Add hour_of_week option to DateTimeFeatures transformer #4724

Merged
merged 12 commits into from Jul 6, 2023
5 changes: 4 additions & 1 deletion sktime/transformations/series/date.py
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
"""Extract calendar features from datetimeindex."""
__author__ = ["danbartl", "KishManani"]
__author__ = ["danbartl", "KishManani", "VyomkeshVyas"]
__all__ = ["DateTimeFeatures"]

import warnings
Expand All @@ -26,6 +26,7 @@
["day", "month", "day", "efficient"],
["day", "week", "weekday", "minimal"],
["hour", "day", "hour", "minimal"],
["hour", "week", "hour_of_week", "comprehensive"],
["minute", "hour", "minute", "minimal"],
["second", "minute", "second", "minimal"],
["millisecond", "second", "millisecond", "minimal"],
Expand Down Expand Up @@ -299,6 +300,8 @@ def _calendar_dummies(x, funcs):
(x["date_sequence"] - quarter_start) / pd.to_timedelta("1D") + 1
).astype(int)
cd = values
elif funcs == "hour_of_week":
cd = date_sequence.day_of_week * 24 + date_sequence.hour
elif funcs == "is_weekend":
cd = date_sequence.day_of_week > 4
else:
Expand Down
23 changes: 23 additions & 0 deletions sktime/transformations/series/tests/test_date.py
Expand Up @@ -122,6 +122,7 @@ def df_panel():
"day_of_month",
"day_of_week",
"hour_of_day",
"hour_of_week",
"minute_of_hour",
"second_of_minute",
"millisecond_of_second",
Expand Down Expand Up @@ -286,3 +287,25 @@ def test_month_of_quarter(df_panel):
yt = t.fit_transform(y)[:13]
expected = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1])
assert (expected == yt.values).all()


@pytest.mark.skipif(
not _check_estimator_deps(DateTimeFeatures, severity="none"),
reason="skip test if required soft dependencies not available",
)
def test_manual_selection_hour_of_week(df_panel):
"""Tests that "hour_of_week" returns correct result in `manual_selection`."""
y = pd.DataFrame(
data={"y": range(6)},
index=pd.date_range(start="2023-01-01", freq="H", periods=6),
)
transformer = DateTimeFeatures(
manual_selection=["hour_of_week"], keep_original_columns=True
)

yt = transformer.fit_transform(y)
expected = pd.DataFrame(
data={"y": range(6), "hour_of_week": [144, 145, 146, 147, 148, 149]},
index=y.index,
)
assert_frame_equal(yt, expected)