{"record":{"id":"4c431f3057449a0d","repo":"OpenBB-finance/OpenBB","slug":"all-columns-must-be-numeric","errorCode":null,"errorMessage":"All columns must be numeric","messagePattern":"All columns must be numeric","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_router.py","lineNumber":256,"sourceCode":"        OBBject with the results being summary object.\n    \"\"\"\n    # pylint: disable=import-outside-toplevel\n    import re  # noqa\n    import statsmodels.api as sm  # noqa\n    from openbb_core.app.utils import (\n        basemodel_to_df,\n        get_target_column,\n        get_target_columns,\n    )\n\n    X = sm.add_constant(get_target_columns(basemodel_to_df(data), x_columns))\n    y = get_target_column(basemodel_to_df(data), y_column)\n\n    try:\n        X = X.astype(float)\n        y = y.astype(float)\n    except ValueError as exc:\n        raise ValueError(\"All columns must be numeric\") from exc\n\n    results = sm.OLS(y, X).fit()\n    results_summary = results.summary()\n    results = {}\n\n    for item in results_summary.tables[0].data:\n        results[item[0].strip()] = item[1].strip()\n        results[item[2].strip()] = str(item[3]).strip()\n\n    table_1 = results_summary.tables[1]\n    headers = table_1.data[0]  # Assuming the headers are in the first row\n    for i, row in enumerate(table_1.data):\n        if i == 0:  # Skipping the header row\n            continue\n        for j, cell in enumerate(row):\n            if j == 0:  # Skipping the row index\n                continue\n            key = f\"{row[0].strip()}_{headers[j].strip()}\"  # Combining row index and column header","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/extensions/econometrics/openbb_econometrics/econometrics_router.py#L238-L274","documentation":"Thrown by the OLS regression endpoint in openbb_econometrics when the selected x_columns/y_column cannot be cast to float via DataFrame.astype(float). statsmodels OLS requires fully numeric design and response matrices, so any non-numeric (string, categorical, date) or NaN-adjacent content in the chosen columns triggers this ValueError, chained from the original pandas cast error.","triggerScenarios":"Calling obb.econometrics.ols() (or the regression router command at line ~256) with y_column or an entry in x_columns referring to a string/categorical/date column; passing a dataset where numeric columns are stored as object dtype after JSON round-tripping.","commonSituations":"Loading data from CSV/JSON where numbers arrive as strings, selecting a date or symbol column as a regressor by mistake, or provider data whose schema changed a field from float to string between versions.","solutions":["Verify dtypes before the call: df.dtypes — only pass float/int columns in x_columns and y_column.","Coerce the source data: df[c] = pd.to_numeric(df[c], errors='coerce') and dropna() before running the regression.","Double-check the column names in x_columns/y_column against the actual dataset columns (get_target_column also fails loudly on missing names).","If a categorical regressor is intended, encode it (dummies) first."],"exampleFix":"# before\nres = obb.econometrics.ols(data, y_column='revenue', x_columns=['sector', 'growth'])  # sector is a string\n\n# after\ndf = data.to_df()\nX = pd.get_dummies(df[['sector']], drop_first=True)\ndf = pd.concat([df[['revenue', 'growth']].apply(pd.to_numeric, errors='coerce'), X], axis=1).dropna()\nres = obb.econometrics.ols(Data(data=df), y_column='revenue', x_columns=['growth', 'sector_technology'])","handlingStrategy":"validation","validationCode":"df = data.to_df()\ncols = [y_column] + list(x_columns)\nassert all(c in df.columns for c in cols), 'missing column(s)'\nnon_numeric = [c for c in cols if not pd.api.types.is_numeric_dtype(df[c])]\nassert not non_numeric, f'non-numeric columns: {non_numeric}'\ndf = df[cols].apply(pd.to_numeric, errors='coerce').dropna()","typeGuard":"def columns_are_numeric(df, columns: list[str]) -> bool:\n    \"\"\"True when every named column exists and has a numeric dtype.\"\"\"\n    return all(c in df.columns and pd.api.types.is_numeric_dtype(df[c]) for c in columns)","tryCatchPattern":"try:\n    res = obb.econometrics.ols(data, y_column=y, x_columns=xs)\nexcept ValueError as e:\n    if str(e) == 'All columns must be numeric':\n        # coerce and retry\n        ...","preventionTips":["Run df.dtypes on the target columns before every regression call.","pd.to_numeric(errors='coerce') + dropna() any stringly-typed numeric columns at ingest.","Encode categoricals with get_dummies instead of passing them raw."],"tags":["econometrics","ols","dtype","regression"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}