HKUDS/Vibe-Trading · error · ValueError

returns and var must cover exactly the same labels; {len(onl

Error message

returns and var must cover exactly the same labels; {len(only_ret)} label(s) only in returns and {len(only_var)} only in var. Align them explicitly -- a partial join silently compares each day against another day's forecast.

What it means

var_backtest's _align helper requires that when both returns and var are pandas Series, their indexes match exactly. Any difference raises ValueError with the count of labels unique to each side, because a partial join would silently compare each day's return against another day's forecast.

Source

Thrown at agent/src/quantlib/var_backtest.py:264

    Returns:
        Tuple of ``(returns, var, index, dropped)``: two equal-length finite
        float arrays, the surviving index when the inputs carried one, and the
        number of pairs dropped for holding a non-finite value.

    Raises:
        ValueError: If either input is not 1-D, if two indexed Series do not
            cover exactly the same labels, if the lengths differ, or if no
            finite pair survives.
    """
    ret_index = returns.index if isinstance(returns, pd.Series) else None
    var_index = var.index if isinstance(var, pd.Series) else None

    if ret_index is not None and var_index is not None:
        if not ret_index.equals(var_index):
            only_ret = ret_index.difference(var_index)
            only_var = var_index.difference(ret_index)
            raise ValueError(
                "returns and var must cover exactly the same labels; "
                f"{len(only_ret)} label(s) only in returns and "
                f"{len(only_var)} only in var. Align them explicitly -- a "
                "partial join silently compares each day against another day's "
                "forecast."
            )

    ret_values = np.asarray(returns, dtype=float)
    if ret_values.ndim > 1:
        raise ValueError(f"returns must be 1-D, got shape {ret_values.shape}")
    ret_values = ret_values.ravel()

    var_values = np.asarray(var, dtype=float)
    if var_values.ndim == 0:
        var_values = np.full(ret_values.shape, float(var_values))
    else:
        if var_values.ndim > 1:
            raise ValueError(f"var must be 1-D or scalar, got shape {var_values.shape}")

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Align explicitly: var = var.reindex(returns.index) after confirming the calendars should match, or join both on a common index.
  2. Regenerate the VaR series from the same returns index so labels match by construction.
  3. Check for duplicate or tz-mismatched index values on both sides.

Example fix

# before
violation_indicator(returns, var)  # ValueError: labels differ
# after
var = var.reindex(returns.index).dropna()
returns = returns.loc[var.index]
violation_indicator(returns, var)
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(returns, pd.Series) and isinstance(var, pd.Series)
assert returns.index.equals(var.index)

Type guard

def indexes_aligned(a, b) -> bool:
    return (not isinstance(a, pd.Series) and not isinstance(b, pd.Series)) or a.index.equals(b.index)

Try / catch

except ValueError as e:
    if 'exactly the same labels' in str(e):
        var = var.reindex(returns.index).dropna(); returns = returns.loc[var.index]

Prevention

When it happens

Trigger: Passing a returns Series and a VaR Series indexed on different date sets — e.g. returns from yfinance (trading days) and VaR computed on a DataFrame that includes a missing day, or one series shifted/reindexed.

Common situations: Merging market data from vendors with different holiday calendars; VaR series produced by a rolling window that drops early NaN rows; reindexing one series but not the other; timezone-aware vs naive DatetimeIndexes.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/30743d29279400e8. Report an issue: GitHub.