{"record":{"id":"6871ff8458d71923","repo":"keras-team/keras","slug":"column-weight-col-weight-col-must-be-numeric","errorCode":null,"errorMessage":"Column weight_col={weight_col} must be numeric.","messagePattern":"Column weight_col=(.+?) must be numeric\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":860,"sourceCode":"                    \"values must be type string, list or tuple.\".format(\n                        self.class_mode, y_col\n                    )\n                )\n        # raise warning if classes are given but will be unused\n        if classes and self.class_mode in {\n            \"input\",\n            \"multi_output\",\n            \"raw\",\n            None,\n        }:\n            warnings.warn(\n                '`classes` will be ignored given the class_mode=\"{}\"'.format(\n                    self.class_mode\n                )\n            )\n        # check that if weight column that the values are numerical\n        if weight_col and not issubclass(df[weight_col].dtype.type, np.number):\n            raise TypeError(f\"Column weight_col={weight_col} must be numeric.\")\n\n    def get_classes(self, df, y_col):\n        labels = []\n        for label in df[y_col]:\n            if isinstance(label, (list, tuple)):\n                labels.append([self.class_indices[lbl] for lbl in label])\n            else:\n                labels.append(self.class_indices[label])\n        return labels\n\n    @staticmethod\n    def _filter_classes(df, y_col, classes):\n        df = df.copy()\n\n        def remove_classes(labels, classes):\n            if isinstance(labels, (list, tuple)):\n                labels = [cls for cls in labels if cls in classes]\n                return labels or None","sourceCodeStart":842,"sourceCodeEnd":878,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L842-L878","documentation":"When weight_col is given to flow_from_dataframe, that DataFrame column must have a numeric dtype so per-sample weights can be used directly.","triggerScenarios":"flow_from_dataframe(..., weight_col='w') where df['w'] is object/string dtype.","commonSituations":"CSV import storing weights as strings; column with mixed types inferred as object.","solutions":["Convert: df[weight_col] = pd.to_numeric(df[weight_col], errors='raise')","Re-export the CSV with numeric weights","Fill/drop NaNs before conversion"],"exampleFix":"// before\ndf['w'] = df['w']  # object dtype strings\n// after\ndf['w'] = pd.to_numeric(df['w'])\n","handlingStrategy":"validation","validationCode":"import numpy as np\nassert np.issubdtype(df[weight_col].dtype, np.number), df[weight_col].dtype","typeGuard":"def is_numeric_col(s): return np.issubdtype(s.dtype, np.number)","tryCatchPattern":"try: flow_from_dataframe(..., weight_col=w)\nexcept TypeError as e: if 'must be numeric' in str(e): df[w] = pd.to_numeric(df[w])","preventionTips":["Use pd.to_numeric on weight columns right after CSV load"],"tags":["keras","dataframe","dtype-validation"],"backgroundTag":"dtype-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}