{"record":{"id":"efcc50ca0bd23808","repo":"pandas-dev/pandas","slug":"column-colname-must-have-a-numeric-dtype-found","errorCode":null,"errorMessage":"Column {colname} must have a numeric dtype. Found '{dtype}' instead","messagePattern":"Column (.+?) must have a numeric dtype\\. Found '(.+?)' instead","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/apply.py","lineNumber":979,"sourceCode":"        pass\n\n    @staticmethod\n    @functools.cache\n    @abc.abstractmethod\n    def generate_numba_apply_func(\n        func, nogil: bool = True, parallel: bool = False\n    ) -> Callable[[npt.NDArray, Index, Index], dict[int, Any]]:\n        pass\n\n    @abc.abstractmethod\n    def apply_with_numba(self):\n        pass\n\n    def validate_values_for_numba(self) -> None:\n        # Validate column dtypes all OK\n        for colname, dtype in self.obj.dtypes.items():\n            if not is_numeric_dtype(dtype):\n                raise ValueError(\n                    f\"Column {colname} must have a numeric dtype. \"\n                    f\"Found '{dtype}' instead\"\n                )\n            if is_extension_array_dtype(dtype):\n                raise ValueError(\n                    f\"Column {colname} is backed by an extension array, \"\n                    f\"which is not supported by the numba engine.\"\n                )\n\n    @abc.abstractmethod\n    def wrap_results_for_axis(\n        self, results: ResType, res_index: Index\n    ) -> DataFrame | Series:\n        pass\n\n    # ---------------------------------------------------------------\n\n    @property","sourceCodeStart":961,"sourceCodeEnd":997,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/apply.py#L961-L997","documentation":"Raised by `validate_values_for_numba` (FrameApply) when at least one column of the DataFrame has a non-numeric dtype and the numba engine was requested. The numba engine compiles a function against raw numeric numpy arrays; object/category/datetime/string columns cannot be passed to numba without conversion.","triggerScenarios":"`df.apply(func, engine='numba', raw=True)` where `df` contains a string, object, datetime, or category column. The loop at apply.py:977 checks each column dtype via `is_numeric_dtype`.","commonSituations":"DataFrame has a hidden index-like column or string labels; mixing numeric and metadata columns; forgetting to select only numeric columns before using the numba engine; numba speedup attempts on heterogeneous frames.","solutions":["Select only numeric columns before applying: `df.select_dtypes(include='number').apply(func, engine='numba', raw=True)`.","Drop or separate non-numeric columns, then recombine results.","Convert where appropriate (e.g. categoricals to codes) — but only if the operation is meaningful."],"exampleFix":"# before\ndf.apply(my_func, engine='numba', raw=True)  # df has string col\n# after\nnum = df.select_dtypes(include='number')\nnum.apply(my_func, engine='numba', raw=True)","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef apply_numba_numeric_only(df, func, **kw):\n    num = df.select_dtypes(include='number')\n    if len(num.columns) != len(df.columns):\n        dropped = set(df.columns) - set(num.columns)\n        raise ValueError(f'non-numeric columns dropped for numba: {dropped}')\n    return num.apply(func, engine='numba', raw=True, **kw)","typeGuard":"def all_columns_numeric(df) -> bool:\n    import pandas as pd\n    return all(pd.api.types.is_numeric_dtype(dt) for dt in df.dtypes)","tryCatchPattern":"try:\n    df.apply(func, engine='numba', raw=True)\nexcept ValueError as e:\n    if 'must have a numeric dtype' in str(e):\n        df.select_dtypes(include='number').apply(func, engine='numba', raw=True)\n    else:\n        raise","preventionTips":["Pre-filter to numeric columns before using the numba engine.","Document numba's numeric-only requirement at the call site."],"tags":["pandas","apply","numba","valueerror","dtype","numeric"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}