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

[DOC] Improve the explanation on the window parameter (offset) of WindowTransformer #1666

Merged
merged 8 commits into from
Apr 7, 2023
3 changes: 2 additions & 1 deletion darts/dataprocessing/transformers/window_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def __init__(

* :``"window"``: Size of the moving window for the "rolling" mode.
If an integer, the fixed number of observations used for each window.
If an offset, the time period of each window.
If an offset, the time period of each window with data type :class:`pandas.Timedelta`
representing a fixed duration.
* :``"min_periods"``: The minimum number of observations in the window required to have a value (otherwise
NaN). Darts reuses pandas defaults of 1 for "rolling" and "expanding" modes and of 0 for "ewm" mode.
* :``"win_type"``: The type of weigthing to apply to the window elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import pandas as pd
import pytest
JQGoh marked this conversation as resolved.
Show resolved Hide resolved

from darts import TimeSeries
from darts.dataprocessing.pipeline import Pipeline
Expand Down Expand Up @@ -442,6 +443,8 @@ class WindowTransformerTestCase(unittest.TestCase):

times = pd.date_range("20130101", "20130110")
target = TimeSeries.from_times_and_values(times, range(1, 11))
times_hourly = pd.date_range(start="20130101", freq="1H", periods=10)
target_hourly = TimeSeries.from_times_and_values(times_hourly, range(1, 11))

series_multi_prob = (
(target + 10)
Expand Down Expand Up @@ -482,6 +485,46 @@ def test_window_transformer_output(self):
transformed_ts_list[1].n_timesteps, self.series_multi_det.n_timesteps
)

def test_window_transformer_offset_parameter(self):
"""
Test that the window parameter can support offset of pandas.Timedelta
"""
base_parameters = {
"function": "mean",
"components": ["0"],
"mode": "rolling",
}

offset_parameters = base_parameters.copy()
offset_parameters.update({"window": pd.Timedelta(hours=4)})
offset_transformer = WindowTransformer(
transforms=offset_parameters,
)
offset_transformed = offset_transformer.transform(self.target_hourly)

integer_parameters = base_parameters.copy()
integer_parameters.update({"window": 4})
integer_transformer = WindowTransformer(
transforms=integer_parameters,
)
integer_transformed = integer_transformer.transform(self.target_hourly)
np.testing.assert_equal(
integer_transformed.values(), offset_transformed.values()
)
self.assertEqual(
offset_transformed.components[0], "rolling_mean_0 days 04:00:00_0"
)
self.assertEqual(integer_transformed.components[0], "rolling_mean_4_0")

invalid_parameters = base_parameters.copy()
invalid_parameters.update({"window": pd.DateOffset(hours=4)})
invalid_transformer = WindowTransformer(
transforms=invalid_parameters,
)
# if pd.DateOffset, raise ValueError of non-fixed frequency
with pytest.raises(ValueError):
JQGoh marked this conversation as resolved.
Show resolved Hide resolved
invalid_transformer.transform(self.target_hourly)

def test_transformers_pipeline(self):
"""
Test that the forecasting window transformer can be used in a pipeline
Expand Down
3 changes: 2 additions & 1 deletion darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3261,7 +3261,8 @@ def window_transform(

* :``"window"``: Size of the moving window for the "rolling" mode.
If an integer, the fixed number of observations used for each window.
If an offset, the time period of each window.
If an offset, the time period of each window with data type :class:`pandas.Timedelta`
representing a fixed duration.
* :``"min_periods"``: The minimum number of observations in the window required to have a value (otherwise
NaN). Darts reuses pandas defaults of 1 for "rolling" and "expanding" modes and of 0 for "ewm" mode.
* :``"win_type"``: The type of weigthing to apply to the window elements.
Expand Down