HKUDS/Vibe-Trading · error · ValueError

returns is empty

Error message

returns is empty

What it means

_align refuses empty input: if the (1-D) returns array has zero elements it raises ValueError('returns is empty'). An empty backtest would otherwise produce division-by-zero statistics and empty reports that look like success.

Solutions

  1. Debug why the returns array is empty (print its length and index range before the call).
  2. Fix date filters / data loading so at least one observation survives.
  3. Guard with a length check and skip the backtest when there is no data.

Example fix

# before
var_backtest(returns[returns.index > '2030-01-01'], var)
# after
sub = returns.loc['2024-01-01':'2024-12-31']
if len(sub):
    var_backtest(sub, var.reindex(sub.index))
Defensive patterns

Strategy: validation

Validate before calling

assert len(returns) > 0, 'no returns in backtest window'

Type guard

def non_empty(x) -> bool:
    try:
        return len(x) > 0
    except TypeError:
        return False

Try / catch

except ValueError as e:
    if str(e) == 'returns is empty': skip_or_raise_data_error(e)

Prevention

When it happens

Trigger: Passing [] or an empty Series/array for returns — e.g. a date filter that selects nothing, an empty CSV read, or a test fixture that failed to populate data.

Common situations: Date-range parameters that exclude all rows; upstream API returning an empty payload; empty DataFrame .iloc[:,0] column selection after a bad filter.

Related errors


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

Appendix: source

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

    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}")
        var_values = var_values.ravel()

    if ret_values.size != var_values.size:
        raise ValueError(
            f"returns and var must be the same length, got {ret_values.size} "
            f"and {var_values.size}"
        )
    if ret_values.size == 0:
        raise ValueError("returns is empty")

    keep = np.isfinite(ret_values) & np.isfinite(var_values)
    dropped = int((~keep).sum())
    if not keep.any():
        raise ValueError("no observation has a finite return and a finite var")

    index = ret_index if ret_index is not None else var_index
    kept_index = index[keep] if index is not None else None
    return ret_values[keep], var_values[keep], kept_index, dropped


def violation_indicator(
    returns: pd.Series | np.ndarray | Sequence[float],
    var: pd.Series | np.ndarray | Sequence[float] | float,
) -> np.ndarray:
    """Flag the days on which the realised loss exceeded the VaR forecast.

    Args:

View on GitHub (pinned to 80ffdda44c)