pola-rs/polars · error · TypeError

cannot convert DataFrame to

Error message

cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)\n{df.schema!r}

What it means

Post-conversion check in frame_to_numpy: after converting the DataFrame to a numpy array the array's dtype came out as `object`, which happens when columns of mixed/incompatible dtypes are concatenated — such arrays cannot be used as Jax/PyTorch tensors. The full schema is included to show which columns caused the object dtype.

Solutions

  1. Cast or select columns so the frame is homogeneous in dtype before converting to numpy/other targets; mixed types yield object dtype.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at py-polars/src/polars/ml/utilities.py:29 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/50311348372f0197. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/ml/utilities.py:29

    *,
    writable: bool,
    target: str,
    order: IndexOrder = "fortran",
) -> np.ndarray[Any, Any]:
    """Convert a DataFrame to a NumPy array for use with Jax or PyTorch."""
    for nm, tp in df.schema.items():
        if tp == List:
            msg = f"cannot convert List column {nm!r} to {target} (use Array dtype instead)"
            raise TypeError(msg) from None

    if df.width == 1 and df.schema.dtypes()[0] == Array:
        arr = df[df.columns[0]].to_numpy(writable=writable)
    else:
        arr = df.to_numpy(writable=writable, order=order)

    if arr.dtype == object:
        msg = f"cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)\n{df.schema!r}"
        raise TypeError(msg)
    return arr

View on GitHub (pinned to 9b5d73fd00)