Skip to content

Conversation

@spolisar
Copy link
Collaborator

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.

@AzulGarza AzulGarza self-requested a review November 22, 2025 01:46
Copy link
Member

@AzulGarza AzulGarza left a 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

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")
works.

note in docstring on higher min series length for Prophet

update error message to reflect Prophet's series length requirements
@AzulGarza AzulGarza requested a review from Copilot November 24, 2025 22:12
Copilot finished reviewing on behalf of AzulGarza November 24, 2025 22:14
Copy link
Contributor

Copilot AI left a 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_windows by 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
Copy link

Copilot AI Nov 24, 2025

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".

Suggested change
# 3 row minimum for a df with Prophet
# Prophet requires min_series_length >= 2h + 1, where h is the forecast horizon
Copilot uses AI. Check for mistakes.
Args:
df (pd.DataFrame):
DataFrame containing the time series to detect anomalies.
Minimum series length is 1 higher for Prophet than other models.
Copy link

Copilot AI Nov 24, 2025

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."

Suggested change
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`.
Copilot uses AI. Check for mistakes.
_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
Copy link

Copilot AI Nov 24, 2025

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).

Suggested change
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
Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants