{"record":{"id":"a0073647bc67d57c","repo":"pola-rs/polars","slug":"invalid-input-for-exclude-n-nexpected-one-or-mor","errorCode":null,"errorMessage":"invalid input for `exclude`\\n\\nExpected one or more `str` or `DataType`; found {item!r} instead.","messagePattern":"invalid input for `exclude`\\\\n\\\\nExpected one or more `str` or `DataType`; found (.+?) instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":586,"sourceCode":"        exclude_dtypes: builtins.list[PolarsDataType] = []\n        for item in (\n            *(\n                columns\n                if isinstance(columns, Collection) and not isinstance(columns, str)\n                else [columns]\n            ),\n            *more_columns,\n        ):\n            if isinstance(item, str):\n                exclude_cols.append(item)\n            elif is_polars_dtype(item):\n                exclude_dtypes.append(item)\n            else:\n                msg = (\n                    \"invalid input for `exclude`\"\n                    f\"\\n\\nExpected one or more `str` or `DataType`; found {item!r} instead.\"\n                )\n                raise TypeError(msg)\n\n        if exclude_cols and exclude_dtypes:\n            msg = \"cannot exclude by both column name and dtype\"\n            raise TypeError(msg)\n\n        excluded = (\n            by_dtype(exclude_dtypes)\n            if exclude_dtypes\n            else Selector._by_name(\n                exclude_cols,\n                strict=False,\n                expand_patterns=True,\n            )\n        )\n        return self - excluded\n\n    def as_expr(self) -> Expr:\n        \"\"\"","sourceCodeStart":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L568-L604","documentation":"Thrown by Selector.exclude (py-polars/src/polars/selectors.py:586) when an argument passed to exclude() is neither a str (column name) nor a Polars DataType. exclude() intentionally accepts only names or dtypes; anything else — ints, None, booleans, numpy scalars — is rejected immediately with a TypeError showing the offending repr. The error occurs at selector construction time, before any DataFrame is touched.","triggerScenarios":"Calling cs.all().exclude(0) (passing a column INDEX), cs.numeric().exclude(None), cs.all().exclude(True), or cs.all().exclude([\"a\", 1]) where the list mixes a valid name with an int. Also cs.all().exclude(pl.Int64(), 5) — the 5 fails even though the dtype is fine.","commonSituations":"Developers coming from a pandas/iloc mindset assume exclude takes positional indices. Others pass a computed list (e.g. from a config file or JSON) containing non-string values like 0 or None, or pass a Python type (str, int) instead of a Polars dtype (pl.String, pl.Int64).","solutions":["Pass only column-name strings or Polars dtypes: cs.all().exclude(\"col_a\", pl.Int64)","To exclude by position, use the index selector instead: cs.all() - cs.by_index(0) or df.select(~cs.by_index(0))","If the argument list is dynamic, sanitize it first: [x for x in items if isinstance(x, str)] and coerce pl.String/int types with pl.String, pl.Int64 before calling exclude","Check the repr in the message to find which item (and where it came from in your data/config) is invalid"],"exampleFix":"# before\ndf.select(cs.all().exclude(0))  # 0 is not a name or dtype\n\n# after\ndf.select(cs.all().exclude(\"col_a\", pl.Int64))\n# or exclude by position:\ndf.select(cs.all() - cs.by_index(0))","handlingStrategy":"type-guard","validationCode":"from polars.selectors import _expand_selector_dtypes  # not public; prefer explicit check\nfrom polars import selectors as cs\n\ndef safe_exclude(sel, *items):\n    for it in items:\n        if not (isinstance(it, str) or pl.api.is_polars_dtype(it)):\n            raise TypeError(f\"exclude only accepts str or Polars dtype, got {it!r}\")\n    return sel.exclude(*items)","typeGuard":"from typing import TypeGuard\nfrom polars.type_aliases import PolarsDataType\n\ndef is_exclude_arg(item: object) -> TypeGuard[str | PolarsDataType]:\n    import polars as pl\n    return isinstance(item, str) or pl.api.is_polars_dtype(item)","tryCatchPattern":"try:\n    sel = base.exclude(*args)\nexcept TypeError as e:\n    if \"invalid input for `exclude`\" in str(e):\n        raise ValueError(f\"bad exclude args {args!r}\") from e\n    raise","preventionTips":["Type exclude() inputs in your code as str | PolarsDataType and validate at the boundary","Never pass positional indices to exclude — use cs.by_index for positions","When exclude lists come from config/JSON, validate them once at load time"],"tags":["polars","selectors","typeerror","api-misuse","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}