{"record":{"id":"a61b1602a222ecee","repo":"pola-rs/polars","slug":"invalid-input-for-col-expected-str-or-dataty","errorCode":null,"errorMessage":"invalid input for `col`\n\nExpected `str` or `DataType`, got {type(name).__name__!r}.","messagePattern":"invalid input for `col`\n\nExpected `str` or `DataType`, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/col.py","lineNumber":68,"sourceCode":"    \"\"\"Create one or more column expressions representing column(s) in a DataFrame.\"\"\"\n    dtypes: list[PolarsDataType]\n    if more_names:\n        if isinstance(name, str):\n            names_str = [name]\n            names_str.extend(more_names)  # type: ignore[arg-type]\n            return pl.Selector._by_name(\n                names_str, strict=True, expand_patterns=True\n            ).as_expr()\n        elif is_polars_dtype(name):\n            dtypes = [name]\n            dtypes.extend(more_names)  # 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                f\"\\n\\nExpected `str` or `DataType`, got {type(name).__name__!r}.\"\n            )\n            raise TypeError(msg)\n\n    if isinstance(name, str):\n        return wrap_expr(plr.col(name))\n    elif is_polars_dtype(name):\n        dtypes = _polars_dtype_match(name)\n        return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]\n    elif isinstance(name, type):\n        dtypes = _python_dtype_match(name)\n        return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]\n    elif isinstance(name, Iterable):\n        names = list(name)\n        if not names:\n            return pl.Selector._by_name(\n                names=names,  # type: ignore[arg-type]\n                strict=True,\n                expand_patterns=True,\n            ).as_expr()\n","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/col.py#L50-L86","documentation":"When pl.col is called with extra positional arguments, the first argument must be a str (column name) or a Polars dtype so a name- or dtype-based selector can be built. Any other first argument alongside more_names raises this TypeError before a selector is created. Python type objects like int are only accepted in the single-argument form.","triggerScenarios":"pl.col(None, 'a', 'b'); pl.col(123, 'x'); splatting a dynamic list where the first element is not a str: pl.col(*names) with names[0] an int; passing the string 'int64' with more names (strings are treated as column names, not dtypes).","commonSituations":"Programmatic column selection where the first element came from untrusted or empty data; mixing dtype classes (pl.Int64) with dtype strings; kwargs/splat forwarding helpers.","solutions":["Make the first argument a column-name str or a real Polars dtype (pl.Int64, not 'int64')","Normalize dynamic lists before splatting: names = [n for n in names if isinstance(n, str)]","For Python type objects drop the extra args: pl.col(int) alone works"],"exampleFix":"# before\npl.col(*cols)  # cols[0] is an int or None\n\n# after\npl.col(*[str(c) for c in cols])\n# or select by dtype with real dtype objects:\npl.col(pl.Int64, pl.Float64)","handlingStrategy":"type-guard","validationCode":"names = [n for n in candidate_names if isinstance(n, str)]\nif not names:\n    raise ValueError('no valid column names supplied')\nexpr = pl.col(*names)","typeGuard":"def is_col_first_arg(x: object) -> bool:\n    return isinstance(x, str) or hasattr(x, '__pl.TimeUnit__') or isinstance(x, type)","tryCatchPattern":null,"preventionTips":["Normalize dynamic name lists to str before splatting into pl.col","Use real dtype objects (pl.Int64), never dtype strings, alongside more_names","Keep a single helper that validates and builds column selectors"],"tags":["polars","column-selection","typeerror","selector","argument-validation"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}