pola-rs/polars · error · TypeError

cannot convert List column

Error message

cannot convert List column {nm!r} to {target} (use Array dtype instead)

What it means

frame_to_numpy (behind to_jax/to_torch) cannot convert List-dtype columns because they would produce ragged/object arrays. It demands fixed-width Array dtype columns (or plain numeric columns) instead.

Solutions

  1. Cast the List column to a fixed-width Array dtype: pl.col("c").list.to_array(width) or df.with_columns(pl.col("c").cast(pl.Array(pl.Int64, width)))
  2. Drop List columns before conversion
  3. Explode/unnest the list column if appropriate

Example fix

// before
df = df.group_by("g", maintain_order=True).agg(pl.col("x"))  # List dtype
arr = df.to_torch()
// after
df = df.with_columns(pl.col("x").list.to_array(3).cast(pl.Array(pl.Float64, 3)))
arr = df.to_torch()
Defensive patterns

Strategy: validation

Validate before calling

if any(tp == pl.List for tp in df.schema.dtypes()):
    df = df.with_columns(pl.col(nm).list.to_array(w).cast(pl.Array(inner, w)) for nm, tp in df.schema.items() if tp == pl.List)
arr = df.to_torch()

Type guard

def has_list_dtype(df) -> bool:
    return any(tp == pl.List for tp in df.schema.dtypes())

Try / catch

try:
    arr = df.to_torch()
except TypeError:
    arr = df.select(pl.exclude([n for n, t in df.schema.items() if t == pl.List])).to_torch()

Prevention

When it happens

Trigger: Calling df.to_torch() or df.to_jax() on a DataFrame containing any column of dtype pl.List.

Common situations: Building feature tensors from nested list columns produced by group_by().agg(pl.col("x")); forgetting to cast to Array(fixed width).

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/1c3c9ab5cafd00a0. Report an issue: GitHub.

Appendix: source

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

from polars import DataFrame
from polars._dependencies import numpy as np
from polars._typing import IndexOrder
from polars.datatypes import Array, List


def frame_to_numpy(
    df: DataFrame,
    *,
    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)