pandas-dev/pandas · error · ValueError

Column {colname} must have a numeric dtype. Found '{dtype}'

Error message

Column {colname} must have a numeric dtype. Found '{dtype}' instead

What it means

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.

Source

Thrown at pandas/core/apply.py:979

        pass

    @staticmethod
    @functools.cache
    @abc.abstractmethod
    def generate_numba_apply_func(
        func, nogil: bool = True, parallel: bool = False
    ) -> Callable[[npt.NDArray, Index, Index], dict[int, Any]]:
        pass

    @abc.abstractmethod
    def apply_with_numba(self):
        pass

    def validate_values_for_numba(self) -> None:
        # Validate column dtypes all OK
        for colname, dtype in self.obj.dtypes.items():
            if not is_numeric_dtype(dtype):
                raise ValueError(
                    f"Column {colname} must have a numeric dtype. "
                    f"Found '{dtype}' instead"
                )
            if is_extension_array_dtype(dtype):
                raise ValueError(
                    f"Column {colname} is backed by an extension array, "
                    f"which is not supported by the numba engine."
                )

    @abc.abstractmethod
    def wrap_results_for_axis(
        self, results: ResType, res_index: Index
    ) -> DataFrame | Series:
        pass

    # ---------------------------------------------------------------

    @property

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Select only numeric columns before applying: `df.select_dtypes(include='number').apply(func, engine='numba', raw=True)`.
  2. Drop or separate non-numeric columns, then recombine results.
  3. Convert where appropriate (e.g. categoricals to codes) — but only if the operation is meaningful.

Example fix

# before
df.apply(my_func, engine='numba', raw=True)  # df has string col
# after
num = df.select_dtypes(include='number')
num.apply(my_func, engine='numba', raw=True)
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd

def apply_numba_numeric_only(df, func, **kw):
    num = df.select_dtypes(include='number')
    if len(num.columns) != len(df.columns):
        dropped = set(df.columns) - set(num.columns)
        raise ValueError(f'non-numeric columns dropped for numba: {dropped}')
    return num.apply(func, engine='numba', raw=True, **kw)

Type guard

def all_columns_numeric(df) -> bool:
    import pandas as pd
    return all(pd.api.types.is_numeric_dtype(dt) for dt in df.dtypes)

Try / catch

try:
    df.apply(func, engine='numba', raw=True)
except ValueError as e:
    if 'must have a numeric dtype' in str(e):
        df.select_dtypes(include='number').apply(func, engine='numba', raw=True)
    else:
        raise

Prevention

When it happens

Trigger: `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`.

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/efcc50ca0bd23808. Report an issue: GitHub.