{"record":{"id":"2d138309edee41fe","repo":"HKUDS/Vibe-Trading","slug":"label-end-times-must-be-1-d-got-shape-span-ends","errorCode":null,"errorMessage":"label_end_times must be 1-D, got shape {span_ends.shape}","messagePattern":"label_end_times must be 1-D, got shape (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/crossvalidation.py","lineNumber":145,"sourceCode":"    Raises:\n        ValueError: If the input is empty, not 1-D, holds a non-finite value, or\n            declares a label ending before it starts.\n    \"\"\"\n    if isinstance(label_end_times, pd.Series):\n        if label_end_times.empty:\n            raise ValueError(\"label_end_times is empty\")\n        starts = label_end_times.index\n        ends = label_end_times.to_numpy()\n        # searchsorted on the start index converts label end *times* into label\n        # end *positions*; the right insertion point minus one keeps a label\n        # that ends between two observations attached to the earlier one.\n        positions = np.searchsorted(starts, ends, side=\"right\") - 1\n        positions = np.clip(positions, np.arange(len(starts)), len(starts) - 1)\n        span_ends = positions.astype(int)\n    else:\n        span_ends = np.asarray(label_end_times, dtype=float)\n        if span_ends.ndim != 1:\n            raise ValueError(f\"label_end_times must be 1-D, got shape {span_ends.shape}\")\n        if span_ends.size == 0:\n            raise ValueError(\"label_end_times is empty\")\n        if not np.isfinite(span_ends).all():\n            raise ValueError(\"label_end_times holds a non-finite value\")\n        span_ends = span_ends.astype(int)\n        if (span_ends < np.arange(span_ends.size)).any():\n            raise ValueError(\n                \"a label cannot end before the observation it belongs to starts\"\n            )\n\n    if n_samples is not None and span_ends.size != n_samples:\n        raise ValueError(\n            f\"label_end_times has {span_ends.size} entries but the sample has {n_samples}\"\n        )\n    return span_ends\n\n\ndef _apply_purge_and_embargo(","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/crossvalidation.py#L127-L163","documentation":"When label_end_times is not a pandas Series, _as_label_spans converts it with np.asarray to a 1-D float array of span end positions. Passing a 2-D array (or any higher-dimensional array) breaks the positional correspondence between labels and observations, so it is rejected with the offending shape in the message.","triggerScenarios":"Calling the purged CV entry points with a numpy array of shape (n, 1), (1, n), or a DataFrame (which converts to 2-D) instead of a 1-D array or Series.","commonSituations":"Column vectors from shape (n,1) produced by .reshape(-1,1) during preprocessing; passing a one-column DataFrame where a Series is expected; batched arrays with a leading batch dimension.","solutions":["Flatten to 1-D: label_end_times = arr.reshape(-1) or arr.ravel()","Select a single column from DataFrames: df['label_end'].values","Verify span_ends.ndim == 1 before calling"],"exampleFix":"# before\nfolds = purged_kfold_splits(ends.reshape(-1, 1), n_splits=5)\n\n# after\nfolds = purged_kfold_splits(ends.reshape(-1), n_splits=5)","handlingStrategy":"validation","validationCode":"ends = np.asarray(label_end_times)\nif ends.ndim != 1:\n    ends = ends.reshape(-1)\nfolds = purged_kfold_splits(ends, n_splits=5)","typeGuard":"def is_1d_array_like(x) -> bool:\n    a = np.asarray(x)\n    return a.ndim == 1","tryCatchPattern":"try:\n    folds = purged_kfold_splits(ends, n_splits=5)\nexcept ValueError as e:\n    if '1-D' in str(e):\n        folds = purged_kfold_splits(np.asarray(ends).reshape(-1), n_splits=5)\n    else:\n        raise","preventionTips":["Never pass (n,1) column vectors; flatten after one-hot/scaling steps","Use df[col].to_numpy() rather than passing the DataFrame itself","Assert ndim == 1 in test fixtures for CV inputs"],"tags":["cross-validation","numpy","dimensionality","input-validation","python"],"backgroundTag":"invalid-array-shape","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}