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

Fix issues related to new Pandas offset names #3117

Merged
merged 12 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
16 changes: 7 additions & 9 deletions src/gluonts/time_feature/lag.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ def _make_lags_for_month(multiple, num_cycles=3):
# normalize offset name, so that both `W` and `W-SUN` refer to `W`
offset_name = norm_freq_str(offset.name)

if offset_name == "A":
if offset_name in ["A", "Y", "YE"]:
lags = []
elif offset_name == "Q":
elif offset_name in ["Q", "QE"]:
assert (
offset.n == 1
), "Only multiple 1 is supported for quarterly. Use x month instead."
lags = _make_lags_for_month(offset.n * 3.0)
elif offset_name == "M":
elif offset_name in ["M", "ME"]:
lags = _make_lags_for_month(offset.n)
elif offset_name == "W":
lags = _make_lags_for_week(offset.n)
Expand All @@ -124,31 +124,29 @@ def _make_lags_for_month(multiple, num_cycles=3):
lags = _make_lags_for_day(
offset.n, days_in_week=5, days_in_month=22
) + _make_lags_for_week(offset.n / 5.0)
elif offset_name == "H":
elif offset_name in ["H", "h"]:
lags = (
_make_lags_for_hour(offset.n)
+ _make_lags_for_day(offset.n / 24)
+ _make_lags_for_week(offset.n / (24 * 7))
)
# minutes
elif offset_name == "T":
elif offset_name in ["T", "min"]:
lags = (
_make_lags_for_minute(offset.n)
+ _make_lags_for_hour(offset.n / 60)
+ _make_lags_for_day(offset.n / (60 * 24))
+ _make_lags_for_week(offset.n / (60 * 24 * 7))
)
# second
elif offset_name == "S":
elif offset_name in ["S", "s"]:
lags = (
_make_lags_for_second(offset.n)
+ _make_lags_for_minute(offset.n / 60)
+ _make_lags_for_hour(offset.n / (60 * 60))
)
else:
raise ValueError(
f"invalid frequency | `freq_str={freq_str}` -> `offset_name={offset_name}`"
)
raise ValueError(f"invalid frequency: {freq_str=}, {offset_name=}")

# flatten lags list and filter
lags = [
Expand Down
6 changes: 5 additions & 1 deletion src/gluonts/time_feature/seasonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

DEFAULT_SEASONALITIES = {
"S": 3600, # 1 hour
"s": 3600, # 1 hour
"T": 1440, # 1 day
"min": 1440, # 1 day
"H": 24, # 1 day
"h": 24, # 1 day
"D": 1, # 1 day
"W": 1, # 1 week
"M": 12,
"ME": 12,
"B": 5,
"Q": 4,
}
Expand All @@ -36,7 +40,7 @@ def get_seasonality(freq: str, seasonalities=DEFAULT_SEASONALITIES) -> int:
"""
Return the seasonality of a given frequency:

>>> get_seasonality("2H")
>>> get_seasonality("2h")
12
"""
offset = pd.tseries.frequencies.to_offset(freq)
Expand Down
16 changes: 8 additions & 8 deletions test/time_feature/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@


def test_norm_freq_str():
assert norm_freq_str(to_offset("Y").name) == "A"
assert norm_freq_str(to_offset("YS").name) == "A"
assert norm_freq_str(to_offset("A").name) == "A"
assert norm_freq_str(to_offset("AS").name) == "A"
assert norm_freq_str(to_offset("Y").name) in ["A", "YE"]
assert norm_freq_str(to_offset("YS").name) in ["A", "Y"]
assert norm_freq_str(to_offset("A").name) in ["A", "YE"]
assert norm_freq_str(to_offset("AS").name) in ["A", "Y"]

assert norm_freq_str(to_offset("Q").name) == "Q"
assert norm_freq_str(to_offset("Q").name) in ["Q", "QE"]
assert norm_freq_str(to_offset("QS").name) == "Q"

assert norm_freq_str(to_offset("M").name) == "M"
assert norm_freq_str(to_offset("MS").name) == "M"
assert norm_freq_str(to_offset("M").name) in ["M", "ME"]
assert norm_freq_str(to_offset("MS").name) in ["M", "ME"]

assert norm_freq_str(to_offset("S").name) == "S"
assert norm_freq_str(to_offset("S").name) in ["S", "s"]
Loading