pola-rs/polars · error · TypeError
cannot convert DataFrame to
Error message
cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)
{df.schema!r} What it means
After conversion, if the NumPy array's dtype is `object`, the DataFrame had mixed-type columns that NumPy can only represent as object arrays, which is unsuitable for Jax/PyTorch. Polars raises instead of silently returning an object array.
Solutions
- Select only numeric columns: df.select(pl.col(pl.Float64), ...) or df.select(pl.numeric())
- Cast columns to a common dtype before conversion
- Drop non-numeric columns like strings/datetimes
Example fix
// before arr = df.to_torch() # df has a 'name' string column // after arr = df.select(pl.numeric()).to_torch()
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
obj_cols = [n for n, t in df.schema.items() if not t.is_numeric()]
if obj_cols:
df = df.select(pl.numeric())
arr = df.to_torch() Type guard
def is_numeric_frame(df) -> bool:
return all(tp.is_numeric() for tp in df.schema.dtypes()) Try / catch
try:
arr = df.to_jax()
except TypeError:
arr = df.select(pl.numeric()).to_jax() Prevention
- Select only numeric columns for tensor conversion
- Drop string/ID/categorical columns before conversion
- Cast mixed numeric dtypes to a common dtype (e.g. Float32)
When it happens
Trigger: Calling df.to_jax()/df.to_torch() on a DataFrame mixing incompatible dtypes (e.g. strings with numbers, or multiple different dtypes that produce object arrays).
Common situations: Feature frames that accidentally include an ID/string column alongside numeric features; datetime + float mixes.
Related errors
- cannot convert List column
- cannot parse numpy data type
- incorrect NumPy datetime resolution 'D' (datetime only)…
- arr.dot query vector must be one-dimensional
- cannot compare datetime.datetime to Series of type
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/f5d84583bd4c78c1.
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 fe841f959e)