pola-rs/polars · error · TypeError
cannot convert DataFrame to {target} (mixed type columns res
Error message
cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)\n{df.schema!r} What it means
After converting a DataFrame to a numpy array for PyTorch/Jax (frame_to_numpy), if the resulting array has dtype object the values cannot be loaded into a typed tensor, so TypeError is raised with the full frame schema printed for diagnosis. Object dtype appears when the frame mixes incompatible column types (e.g. String alongside numeric) or contains object columns.
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 df599052da)
Solutions
- Select only numeric feature columns before converting: df.select(cs.numeric()).to_torch()
- Cast to a common numeric dtype: df.select(pl.col(c).cast(pl.Float32) for c in cols)
- Encode string/categorical columns first (e.g. to codes or one-hot), or exclude them
- Read the schema printed in the error to find the offending non-numeric column
Example fix
# before df.to_torch() # frame has 'name': String next to numeric columns # after import polars.selectors as cs df.select(cs.numeric()).cast(pl.Float32).to_torch()
Defensive patterns
Strategy: validation
Validate before calling
import polars.selectors as cs
numeric = df.select(cs.numeric())
if numeric.width != df.width:
bad = [c for c in df.columns if c not in numeric.columns]
raise TypeError(f'non-numeric columns block tensor conversion: {bad}') Type guard
import polars as pl
import polars.selectors as cs
def all_numeric(df: pl.DataFrame) -> bool:
return df.width == df.select(cs.numeric()).width Prevention
- Select and cast features explicitly before to_torch/to_jax; never pass raw inferred frames
- Encode labels (strings) to integers before tensor conversion
When it happens
Trigger: df.to_torch() / df.to_jax(return_type='array') where the frame has heterogeneous column dtypes (strings + numbers), pl.Object columns, or unconverted categorical/string features.
Common situations: Feeding raw CSV/inferred frames to ML conversion without selecting/casting features; string label columns left in the frame; mixed-type columns from dirty data.
Related errors
- cannot convert List column {nm!r} to {target} (use Array dty
- cannot treat NumPy array of type {arr.dtype} as indices
- could not find `apply_ufunc_{numpy_char_code_to_dtype(dtype_
- cannot select columns using NumPy array of type {key.dtype}
- only 1D NumPy arrays can be treated as indices
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/50311348372f0197.
Report an issue: GitHub.