{"record":{"id":"30743d29279400e8","repo":"HKUDS/Vibe-Trading","slug":"returns-and-var-must-cover-exactly-the-same-labels","errorCode":null,"errorMessage":"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.","messagePattern":"returns and var must cover exactly the same labels; (.+?) label\\(s\\) only in returns and (.+?) only in var\\. Align them explicitly -- a partial join silently compares each day against another day's forecast\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/var_backtest.py","lineNumber":264,"sourceCode":"\n    Returns:\n        Tuple of ``(returns, var, index, dropped)``: two equal-length finite\n        float arrays, the surviving index when the inputs carried one, and the\n        number of pairs dropped for holding a non-finite value.\n\n    Raises:\n        ValueError: If either input is not 1-D, if two indexed Series do not\n            cover exactly the same labels, if the lengths differ, or if no\n            finite pair survives.\n    \"\"\"\n    ret_index = returns.index if isinstance(returns, pd.Series) else None\n    var_index = var.index if isinstance(var, pd.Series) else None\n\n    if ret_index is not None and var_index is not None:\n        if not ret_index.equals(var_index):\n            only_ret = ret_index.difference(var_index)\n            only_var = var_index.difference(ret_index)\n            raise ValueError(\n                \"returns and var must cover exactly the same labels; \"\n                f\"{len(only_ret)} label(s) only in returns and \"\n                f\"{len(only_var)} only in var. Align them explicitly -- a \"\n                \"partial join silently compares each day against another day's \"\n                \"forecast.\"\n            )\n\n    ret_values = np.asarray(returns, dtype=float)\n    if ret_values.ndim > 1:\n        raise ValueError(f\"returns must be 1-D, got shape {ret_values.shape}\")\n    ret_values = ret_values.ravel()\n\n    var_values = np.asarray(var, dtype=float)\n    if var_values.ndim == 0:\n        var_values = np.full(ret_values.shape, float(var_values))\n    else:\n        if var_values.ndim > 1:\n            raise ValueError(f\"var must be 1-D or scalar, got shape {var_values.shape}\")","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/var_backtest.py#L246-L282","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align explicitly: var = var.reindex(returns.index) after confirming the calendars should match, or join both on a common index.","Regenerate the VaR series from the same returns index so labels match by construction.","Check for duplicate or tz-mismatched index values on both sides."],"exampleFix":"# before\nviolation_indicator(returns, var)  # ValueError: labels differ\n# after\nvar = var.reindex(returns.index).dropna()\nreturns = returns.loc[var.index]\nviolation_indicator(returns, var)","handlingStrategy":"validation","validationCode":"assert isinstance(returns, pd.Series) and isinstance(var, pd.Series)\nassert returns.index.equals(var.index)","typeGuard":"def indexes_aligned(a, b) -> bool:\n    return (not isinstance(a, pd.Series) and not isinstance(b, pd.Series)) or a.index.equals(b.index)","tryCatchPattern":"except ValueError as e:\n    if 'exactly the same labels' in str(e):\n        var = var.reindex(returns.index).dropna(); returns = returns.loc[var.index]","preventionTips":["Always reindex both series to a shared calendar before backtesting","Standardize timezones and drop duplicate index entries at load time"],"tags":["var-backtest","pandas","index-alignment"],"backgroundTag":"index-mismatch","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}