{"record":{"id":"ccaa23fe1c1fbef3","repo":"pola-rs/polars","slug":"invalid-input-for-col-expected-iterable-of-type","errorCode":null,"errorMessage":"invalid input for `col`\n\nExpected iterable of type `str` or `DataType`, got iterable of type {type(item).__name__!r}.","messagePattern":"invalid input for `col`\n\nExpected iterable of type `str` or `DataType`, got iterable of type (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/col.py","lineNumber":110,"sourceCode":"                expand_patterns=True,\n            ).as_expr()\n        elif is_polars_dtype(item):\n            dtypes = []\n            for nm in names:\n                dtypes.extend(_polars_dtype_match(nm))  # type: ignore[arg-type]\n            return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]\n        elif isinstance(item, type):\n            dtypes = []\n            for nm in names:\n                dtypes.extend(_python_dtype_match(nm))  # type: ignore[arg-type]\n            return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]\n        else:\n            msg = (\n                \"invalid input for `col`\"\n                \"\\n\\nExpected iterable of type `str` or `DataType`,\"\n                f\" got iterable of type {type(item).__name__!r}.\"\n            )\n            raise TypeError(msg)\n    else:\n        msg = (\n            \"invalid input for `col`\"\n            f\"\\n\\nExpected `str` or `DataType`, got {type(name).__name__!r}.\"\n        )\n        raise TypeError(msg)\n\n\nif sys.version_info >= (3, 11):\n    # note: using `co_qualname` is more robust; can additionally\n    # detect class scope from inside classmethods and staticmethods...\n    def _get_class_objname(f: FrameType) -> str:\n        return f.f_code.co_qualname.split(\".\")[-2:][0]\n\n    _have_qualname = True\nelse:\n    # ... but it's not available until 3.11\n    def _get_class_objname(f: FrameType) -> str:","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/col.py#L92-L128","documentation":"When pl.col receives an iterable, it dispatches on the type of the first element: all-str means a name selector, dtype or Python class means a dtype selector. If the first element is neither a str, a Polars dtype, nor a class, this TypeError names the offending element type. Note that only the first element is inspected for dispatch — mixed lists can still misbehave.","triggerScenarios":"pl.col([1, 2, 3]); pl.col([None]); pl.col([b'a']) (bytes names); pl.col((1,)) tuple of indices; iterators whose first yielded value is not a str.","commonSituations":"Passing column indices instead of names (pandas/NumPy habit); bytes column names from serialized sources; lists built by appending an int sentinel first; empty-then-filled lists from loops.","solutions":["Convert entries to strings: pl.col([str(c) for c in cols])","Map positions to real names via df.columns[i] before calling pl.col","For dtype selection pass actual dtype objects: pl.col([pl.Int64, pl.Float64])","Keep name lists homogeneous str from the start"],"exampleFix":"# before\npl.col([0, 1, 2])\n\n# after\npl.col([df.columns[i] for i in (0, 1, 2)])\n# or simply\npl.col(['a', 'b', 'c'])","handlingStrategy":"type-guard","validationCode":"cols = [str(c) for c in raw_cols]\nif not all(isinstance(c, str) for c in cols):\n    raise TypeError('pl.col iterable items must be str or dtypes')\nexpr = pl.col(cols)","typeGuard":"def is_homogeneous_name_list(xs: object) -> bool:\n    return isinstance(xs, (list, tuple)) and bool(xs) and all(isinstance(x, str) for x in xs)","tryCatchPattern":null,"preventionTips":["Translate column indices via df.columns before pl.col","Keep name lists str-only from construction","Reject bytes names at the ingestion boundary"],"tags":["polars","column-selection","typeerror","selector","iterable"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}