{"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":"exception","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/ml/utilities.py#L2-L31","documentation":"frame_to_numpy (backing DataFrame.to_torch and DataFrame.to_jax) must produce a rectangular, fixed-stride array. A variable-length List(pl.List) column has no fixed shape, so conversion fails with TypeError telling you to use the fixed-shape Array dtype (pl.Array(inner, width)) instead.","triggerScenarios":"df.to_torch() or df.to_jax(return_type='array') on a frame containing a pl.List column; list columns produced by agg(...implode()), list literals, or str.split.","commonSituations":"ML feature frames with ragged per-row sequences (tokens, sensors, histories) fed straight into to_torch/to_jax without padding.","solutions":["Pad to a fixed width and cast: pl.col('x').list.eval(...).cast(pl.Array(inner, width)) or df.cast({'x': pl.Array(pl.Int64, 3)})","Drop or explode the list column before conversion if it is not a feature","Convert the list column separately (e.g. row-by-row tensors) and keep the rectangular frame for the rest"],"exampleFix":"# before\npl.DataFrame({'x': [[1, 2], [3]]}).to_torch()  # List dtype -> TypeError\n\n# after\npl.DataFrame({'x': [[1, 2], [3]]}).with_columns(\n    pl.col('x').list.pad_end(3).cast(pl.Array(pl.Int64, 3))\n).to_torch()","handlingStrategy":"validation","validationCode":"list_cols = [name for name, tp in df.schema.items() if tp == pl.List]\nif list_cols:\n    raise TypeError(f'List columns cannot become tensors: {list_cols}; cast to pl.Array first')","typeGuard":"import polars as pl\n\ndef is_tensor_convertible(df: pl.DataFrame) -> bool:\n    return all(tp != pl.List for tp in df.schema.values())","tryCatchPattern":null,"preventionTips":["Make ragged columns fixed-width (list.pad_end + cast to pl.Array) before to_torch/to_jax","Check df.schema for pl.List before ML export in data-validation steps"],"tags":["polars","torch","jax","numpy","dtype","conversion"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}