-
-
Notifications
You must be signed in to change notification settings - Fork 44
fix Prophet insufficient data error in detect_anomalies #269
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the pr, Shane! it looks good so far and i agree about making the minimum explicit. would you mind adding a note on the docstrings about this behavior when using Prophet?
also, it'd be super helpful if you could add tests for this fix. something similar to
timecopilot/tests/models/utils/test_forecaster.py
Lines 324 to 334 in 9f83ec7
| def test_detect_anomalies_short_series_error(): | |
| model = SeasonalNaive() | |
| 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=5, freq="D") |
note in docstring on higher min series length for Prophet update error message to reflect Prophet's series length requirements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR addresses issue #258 by fixing an error in detect_anomalies where Prophet models would fail when cross-validation resulted in single-row dataframes. The fix decrements the maximum number of windows for Prophet models to ensure at least 2 rows are passed to the model.
Key changes:
- Added Prophet-specific logic to reduce
max_possible_windowsby 1, ensuring minimum 2-row dataframes for cross-validation - Updated error message to reflect different minimum series length requirements for Prophet vs other models
- Added test case to verify Prophet raises appropriate error for short series
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
timecopilot/models/utils/forecaster.py |
Implements Prophet-specific logic to decrement max windows and updates error messaging for minimum series length requirements |
tests/models/utils/test_forecaster.py |
Adds test case verifying Prophet correctly raises ValueError for insufficient data |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| 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 |
Copilot
AI
Nov 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment "3 row minimum for a df with Prophet" is imprecise. The actual minimum depends on the forecast horizon h. For h=1, the minimum is 3 rows, but for h=2, it would be 5 rows (2h+1). Consider updating the comment to clarify this relationship, e.g., "Prophet requires min_series_length >= 2h + 1".
| # 3 row minimum for a df with Prophet | |
| # Prophet requires min_series_length >= 2h + 1, where h is the forecast horizon |
| Args: | ||
| df (pd.DataFrame): | ||
| DataFrame containing the time series to detect anomalies. | ||
| Minimum series length is 1 higher for Prophet than other models. |
Copilot
AI
Nov 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation comment is unclear and potentially misleading. It states "Minimum series length is 1 higher for Prophet than other models" but doesn't specify what that minimum is. Consider being more explicit, e.g., "For Prophet models, the minimum series length is h + 2 instead of h + 1."
| Minimum series length is 1 higher for Prophet than other models. | |
| For Prophet models, the minimum series length is `h + 2`; for other models, it is `h + 1`. |
| _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 |
Copilot
AI
Nov 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states "min series length should be 1 higher for Prophet than other models" which is correct in concept, but the actual implementation in the next line has the conditional logic inverted (Prophet gets h + 1 when it should get h + 2).
| 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 |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
For #258
In some cases, detect_anomalies can pass a number of windows as an argument to cross_validation that results in a dataframe with a single row being passed to a model. Prophet requires at least 2 rows to function properly.
Decrementing the max possible windows for Prophet fixes that by guaranteeing that the minimum number of rows passed to the model is 2.
There is an implicit minimum of 3 rows in a dataframe used with Prophet for detect_anomalies to function properly. It may be good to make minimums like that explicit.