HKUDS/Vibe-Trading · error · ValueError
ts_mean window must be >= 1, got {n}
Error message
ts_mean window must be >= 1, got {n} What it means
ts_mean requires n >= 1; a rolling mean over zero or negative observations is undefined, so the guard raises immediately. Warmup rows (first n-1) are NaN because min_periods=n.
Source
Thrown at agent/src/factors/base.py:182
def ts_cov(x: pd.DataFrame, y: pd.DataFrame, n: int) -> pd.DataFrame:
"""Rolling sample covariance per column, min_periods=n."""
if n < 2:
raise ValueError(f"ts_cov window must be >= 2, got {n}")
x = _as_float(x)
y = _as_float(y)
cols = x.columns.union(y.columns)
xa = x.reindex(columns=cols)
ya = y.reindex(columns=cols)
cov = xa.rolling(window=n, min_periods=n).cov(ya)
return cov.replace([np.inf, -np.inf], np.nan)
def ts_mean(df: pd.DataFrame, n: int) -> pd.DataFrame:
"""Rolling mean per column, warmup → NaN."""
if n < 1:
raise ValueError(f"ts_mean window must be >= 1, got {n}")
return df.rolling(window=n, min_periods=n).mean()
def ts_std(df: pd.DataFrame, n: int) -> pd.DataFrame:
"""Rolling sample std (ddof=1) per column, warmup → NaN."""
if n < 2:
raise ValueError(f"ts_std window must be >= 2, got {n}")
return df.rolling(window=n, min_periods=n).std(ddof=1)
def ts_max(df: pd.DataFrame, n: int) -> pd.DataFrame:
"""Rolling max per column, warmup → NaN."""
if n < 1:
raise ValueError(f"ts_max window must be >= 1, got {n}")
return df.rolling(window=n, min_periods=n).max()
def ts_min(df: pd.DataFrame, n: int) -> pd.DataFrame:View on GitHub (pinned to 80ffdda44c)
Solutions
- Pass a positive integer window
- Validate window parameters at config load time
- When deriving windows from data length, assert n >= 1 before calling
Example fix
# before m = ts_mean(df, n=0) # after m = ts_mean(df, n=20)
Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(n, int) or n < 1:
raise ValueError(f'invalid ts_mean window: {n!r}')
m = ts_mean(df, n) Type guard
def is_valid_ts_mean_window(n) -> bool:
return isinstance(n, int) and n >= 1 Prevention
- Validate window params at config load, not per call
- Reject fractional/percentage windows early
- Assert n <= len(df) when windows are derived from data length
When it happens
Trigger: ts_mean(df, 0), ts_mean(df, -3), or a window computed as len(df) - offset that hits 0. Called from compute() in the factor pipeline.
Common situations: Missing config values defaulting to 0; percentage windows (0.5) instead of counts; dynamic windows on very short dataframes.
Related errors
- ts_rank window must be >= 1, got {n}
- ts_corr window must be >= 2, got {n}
- ts_cov window must be >= 2, got {n}
- ts_std window must be >= 2, got {n}
- granger_test needs max_lag >= 1, got {max_lag}
AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28).
Data as JSON: /api/errors/79f5ec433ac0957b.
Report an issue: GitHub.