{"record":{"id":"8798b087309def5a","repo":"HKUDS/Vibe-Trading","slug":"returns-must-be-1-d-got-shape-ret-values-shape","errorCode":null,"errorMessage":"returns must be 1-D, got shape {ret_values.shape}","messagePattern":"returns must be 1-D, got shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/var_backtest.py","lineNumber":274,"sourceCode":"    \"\"\"\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}\")\n        var_values = var_values.ravel()\n\n    if ret_values.size != var_values.size:\n        raise ValueError(\n            f\"returns and var must be the same length, got {ret_values.size} \"\n            f\"and {var_values.size}\"\n        )\n    if ret_values.size == 0:\n        raise ValueError(\"returns is empty\")\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/var_backtest.py#L256-L292","documentation":"After index checks, _align converts returns to a 1-D float array; if it has more than one dimension (a DataFrame, a 2-D ndarray, or a (n,1) column vector) it raises ValueError with the offending shape. Backtesting logic compares each return scalar against one VaR scalar, so 2-D input is ambiguous.","triggerScenarios":"Passing a pandas DataFrame of returns instead of a Series, or an ndarray with shape (n,1) from a model output; passing multiple asset return columns at once.","commonSituations":"Portfolio backtests where returns is a wide DataFrame; sklearn/statsmodels wrappers returning 2-D arrays; selecting a column but keeping shape via [[...]] indexing.","solutions":["Select a single column/Series: returns['AAPL'] or returns[:, 0].","Reshape 2-D arrays: returns.reshape(-1) or np.ravel(returns).","If backtesting a portfolio, aggregate to portfolio returns first, then call var_backtest per series."],"exampleFix":"# before\nvar_backtest(returns_df, var_series)  # shape (500, 3)\n# after\nvar_backtest(returns_df['portfolio'], var_series)  # 1-D","handlingStrategy":"type-guard","validationCode":"import numpy as np\nassert np.asarray(returns, dtype=float).ndim <= 1","typeGuard":"def is_1d(x) -> bool:\n    import numpy as np\n    return np.asarray(x).ndim <= 1","tryCatchPattern":"except ValueError as e:\n    if 'must be 1-D' in str(e) and 'returns' in str(e): returns = np.ravel(returns)","preventionTips":["Select single DataFrame columns with df['col'], not df[['col']]","ravel() model outputs that come back 2-D"],"tags":["var-backtest","numpy","shape-mismatch"],"backgroundTag":"dimensionality-mismatch","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}