{"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":"validation","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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/col.py#L92-L128","documentation":"pl.col with an iterable argument validates each item is str or DataType; an item of any other type raises TypeError naming the iterable's item type, since selectors can only be built from names or dtypes.","triggerScenarios":"Calling `pl.col(['a', 1])` or `pl.col([pl.Int64, 'bad', None])` where at least one element is neither str nor polars dtype.","commonSituations":"Building column lists dynamically where a None or numeric slipped in; mixing numpy string types (np.str_) or custom wrappers with real strs.","solutions":["Ensure every element is a `str` or polars DataType","Filter/coerce the list first, e.g. `[str(x) for x in names]`","Check elements with `isinstance(x, str) or is_polars_dtype(x)` before calling"],"exampleFix":"// before\npl.col(['a', 'b', None])\n// after\npl.col(['a', 'b'])","handlingStrategy":"validation","validationCode":"bad = [x for x in items if not (isinstance(x, str) or is_polars_dtype(x))]\nif bad:\n    raise TypeError(f'non-str/dtype items in column list: {bad}')","typeGuard":"def is_valid_col_list(items) -> TypeGuard[list[str]]:\n    return all(isinstance(x, str) for x in items)","tryCatchPattern":"try:\n    expr = pl.col(items)\nexcept TypeError:\n    expr = pl.col([x for x in items if x is not None])","preventionTips":["Sanitize dynamically built column lists (drop None, coerce to str)","Write a unit test asserting column-list contents before building expressions"],"tags":["polars","type-error","iterable","python"],"backgroundTag":"type-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"}