{"record":{"id":"1c3c9ab5cafd00a0","repo":"pola-rs/polars","slug":"cannot-convert-list-column-nm-r-to-target-use","errorCode":null,"errorMessage":"cannot convert List column {nm!r} to {target} (use Array dtype instead)","messagePattern":"cannot convert List column (.+?) to (.+?) \\(use Array dtype instead\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/ml/utilities.py","lineNumber":20,"sourceCode":"\nfrom polars import DataFrame\nfrom polars._dependencies import numpy as np\nfrom polars._typing import IndexOrder\nfrom polars.datatypes import Array, List\n\n\ndef frame_to_numpy(\n    df: DataFrame,\n    *,\n    writable: bool,\n    target: str,\n    order: IndexOrder = \"fortran\",\n) -> np.ndarray[Any, Any]:\n    \"\"\"Convert a DataFrame to a NumPy array for use with Jax or PyTorch.\"\"\"\n    for nm, tp in df.schema.items():\n        if tp == List:\n            msg = f\"cannot convert List column {nm!r} to {target} (use Array dtype instead)\"\n            raise TypeError(msg) from None\n\n    if df.width == 1 and df.schema.dtypes()[0] == Array:\n        arr = df[df.columns[0]].to_numpy(writable=writable)\n    else:\n        arr = df.to_numpy(writable=writable, order=order)\n\n    if arr.dtype == object:\n        msg = f\"cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)\\n{df.schema!r}\"\n        raise TypeError(msg)\n    return arr\n","sourceCodeStart":2,"sourceCodeEnd":31,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/ml/utilities.py#L2-L31","documentation":"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.","triggerScenarios":"Calling df.to_torch() or df.to_jax() on a DataFrame containing any column of dtype pl.List.","commonSituations":"Building feature tensors from nested list columns produced by group_by().agg(pl.col(\"x\")); forgetting to cast to Array(fixed width).","solutions":["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)))","Drop List columns before conversion","Explode/unnest the list column if appropriate"],"exampleFix":"// before\ndf = df.group_by(\"g\", maintain_order=True).agg(pl.col(\"x\"))  # List dtype\narr = df.to_torch()\n// after\ndf = df.with_columns(pl.col(\"x\").list.to_array(3).cast(pl.Array(pl.Float64, 3)))\narr = df.to_torch()","handlingStrategy":"validation","validationCode":"if any(tp == pl.List for tp in df.schema.dtypes()):\n    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)\narr = df.to_torch()","typeGuard":"def has_list_dtype(df) -> bool:\n    return any(tp == pl.List for tp in df.schema.dtypes())","tryCatchPattern":"try:\n    arr = df.to_torch()\nexcept TypeError:\n    arr = df.select(pl.exclude([n for n, t in df.schema.items() if t == pl.List])).to_torch()","preventionTips":["Avoid List columns in feature frames; use fixed-width Array","Cast list.aggregations results to Array before tensor conversion","Validate df.schema before calling to_jax/to_torch"],"tags":["python","polars","numpy","dtype"],"backgroundTag":"unsupported-dtype","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}