{"record":{"id":"f5d84583bd4c78c1","repo":"pola-rs/polars","slug":"cannot-convert-dataframe-to-target-mixed-type-columns-result","errorCode":null,"errorMessage":"cannot convert DataFrame to {target} (mixed type columns result in `object` dtype)\n{df.schema!r}","messagePattern":"cannot convert DataFrame to (.+?) \\(mixed type columns result in `object` dtype\\)\n(.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/ml/utilities.py","lineNumber":29,"sourceCode":"    *,\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":11,"sourceCodeEnd":31,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/ml/utilities.py#L11-L31","documentation":"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.","triggerScenarios":"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).","commonSituations":"Feature frames that accidentally include an ID/string column alongside numeric features; datetime + float mixes.","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"],"exampleFix":"// before\narr = df.to_torch()  # df has a 'name' string column\n// after\narr = df.select(pl.numeric()).to_torch()","handlingStrategy":"validation","validationCode":"import numpy as np\nobj_cols = [n for n, t in df.schema.items() if not t.is_numeric()]\nif obj_cols:\n    df = df.select(pl.numeric())\narr = df.to_torch()","typeGuard":"def is_numeric_frame(df) -> bool:\n    return all(tp.is_numeric() for tp in df.schema.dtypes())","tryCatchPattern":"try:\n    arr = df.to_jax()\nexcept TypeError:\n    arr = df.select(pl.numeric()).to_jax()","preventionTips":["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)"],"tags":["python","polars","numpy","dtype"],"backgroundTag":"dtype-mismatch","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"}