From 0746cf2eead7fcd069aa173c31532c3fac31c272 Mon Sep 17 00:00:00 2001 From: spolisar <22416070+spolisar@users.noreply.github.com> Date: Fri, 21 Nov 2025 13:14:45 -0500 Subject: [PATCH 1/4] fix Prophet insufficient data error in detect_anomalies --- timecopilot/models/utils/forecaster.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/timecopilot/models/utils/forecaster.py b/timecopilot/models/utils/forecaster.py index eb1b5df..e0f4f8c 100644 --- a/timecopilot/models/utils/forecaster.py +++ b/timecopilot/models/utils/forecaster.py @@ -9,6 +9,7 @@ from gluonts.time_feature.seasonality import ( get_seasonality as _get_seasonality, ) +from prophet import Prophet as ProphetBase from scipy import stats from tqdm import tqdm from utilsforecast.plotting import plot_series @@ -348,6 +349,9 @@ def detect_anomalies( min_series_length = df.groupby("unique_id").size().min() # we require at least one observation before the first forecast max_possible_windows = (min_series_length - 1) // h + # 3 row minimum for a df with Prophet + if isinstance(self, ProphetBase): + max_possible_windows -= 1 if n_windows is None: _n_windows = max_possible_windows else: From 4af3f436a9343b965ca9df2cfc4d9bd337fc0e0b Mon Sep 17 00:00:00 2001 From: spolisar <22416070+spolisar@users.noreply.github.com> Date: Mon, 24 Nov 2025 13:57:45 -0500 Subject: [PATCH 2/4] min series length requirements for Prophet in detect_anomalies note in docstring on higher min series length for Prophet update error message to reflect Prophet's series length requirements --- timecopilot/models/utils/forecaster.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/timecopilot/models/utils/forecaster.py b/timecopilot/models/utils/forecaster.py index e0f4f8c..f7b9c54 100644 --- a/timecopilot/models/utils/forecaster.py +++ b/timecopilot/models/utils/forecaster.py @@ -305,6 +305,7 @@ def detect_anomalies( Args: df (pd.DataFrame): DataFrame containing the time series to detect anomalies. + Minimum series length is 1 higher for Prophet than other models. h (int, optional): Forecast horizon specifying how many future steps to predict. In each cross validation window. If not provided, the seasonality @@ -357,9 +358,11 @@ def detect_anomalies( else: _n_windows = min(n_windows, max_possible_windows) if _n_windows < 1: + # min series length should be 1 higher for Prophet than other models + exp_min_series_length = h + 1 if isinstance(self, ProphetBase) else h + 2 raise ValueError( f"Cannot perform anomaly detection: series too short. " - f"Minimum series length required: {h + 1}, " + f"Minimum series length required: {exp_min_series_length}, " f"actual minimum length: {min_series_length}" ) cv_results = self.cross_validation( From e8cdfc2099d4668b20df7722c86f534005cabfea Mon Sep 17 00:00:00 2001 From: spolisar <22416070+spolisar@users.noreply.github.com> Date: Mon, 24 Nov 2025 14:00:53 -0500 Subject: [PATCH 3/4] add test for short series error with Prophet in detect_anomalies --- tests/models/utils/test_forecaster.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/models/utils/test_forecaster.py b/tests/models/utils/test_forecaster.py index f387501..2b96d45 100644 --- a/tests/models/utils/test_forecaster.py +++ b/tests/models/utils/test_forecaster.py @@ -2,6 +2,7 @@ import pytest from utilsforecast.data import generate_series +from timecopilot.models.prophet import Prophet from timecopilot.models.stats import SeasonalNaive from timecopilot.models.utils.forecaster import ( QuantileConverter, @@ -332,3 +333,16 @@ def test_detect_anomalies_short_series_error(): ) with pytest.raises(ValueError, match="Cannot perform anomaly detection"): model.detect_anomalies(df, h=5, freq="D") + + +def test_prophet_detect_anomalies_short_series_error(): + model = Prophet() + df = pd.DataFrame( + { + "unique_id": ["A", "A"], + "ds": pd.date_range("2023-01-01", periods=2, freq="D"), + "y": [1.0, 2.0], + } + ) + with pytest.raises(ValueError, match="Cannot perform anomaly detection"): + model.detect_anomalies(df, h=1, freq="D") From 14fbf53723af142f8772cca17e5a347b5e506d39 Mon Sep 17 00:00:00 2001 From: spolisar Date: Mon, 24 Nov 2025 18:53:09 -0500 Subject: [PATCH 4/4] fix: fix ternary operator error in detect_anomalies error message Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- timecopilot/models/utils/forecaster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/timecopilot/models/utils/forecaster.py b/timecopilot/models/utils/forecaster.py index f7b9c54..2201e49 100644 --- a/timecopilot/models/utils/forecaster.py +++ b/timecopilot/models/utils/forecaster.py @@ -359,7 +359,7 @@ def detect_anomalies( _n_windows = min(n_windows, max_possible_windows) if _n_windows < 1: # min series length should be 1 higher for Prophet than other models - exp_min_series_length = h + 1 if isinstance(self, ProphetBase) else h + 2 + exp_min_series_length = h + 2 if isinstance(self, ProphetBase) else h + 1 raise ValueError( f"Cannot perform anomaly detection: series too short. " f"Minimum series length required: {exp_min_series_length}, "