{"record":{"id":"2d4cd8a5c3df5f80","repo":"pola-rs/polars","slug":"cannot-exclude-by-both-column-name-and-dtype","errorCode":null,"errorMessage":"cannot exclude by both column name and dtype","messagePattern":"cannot exclude by both column name and dtype","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":590,"sourceCode":"                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        \"\"\"\n        Materialize the `selector` as a normal expression.\n\n        This ensures that the operators `|`, `&`, `~` and `-`\n        are applied on the data and not on the selector sets.","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L572-L608","documentation":"Thrown by Selector.exclude (py-polars/src/polars/selectors.py:590) when a single exclude() call receives BOTH column-name strings AND dtypes, e.g. exclude(\"a\", pl.Int64). The implementation must build either a by-name or a by-dtype exclusion selector, so mixing the two kinds in one call is ambiguous and rejected with TypeError. Note this fires only when at least one of each kind is present in the same call.","triggerScenarios":"cs.all().exclude(\"foo\", pl.Utf8), cs.numeric().exclude([\"bar\", pl.Float64]), or cs.all().exclude(*names, *dtypes) where names is a list of strings and dtypes a list of DataType instances.","commonSituations":"Dynamically building an exclusion list from two sources (user-supplied column names plus a fixed dtype list) and splatting them into one exclude() call. Refactors that merge two exclude calls into one also trip this.","solutions":["Chain two exclude calls: cs.all().exclude(\"foo\").exclude(pl.Utf8)","Or compose a single complement selector: cs.all() - (cs.by_name(\"foo\") | cs.by_dtype(pl.Utf8))","If args are dynamic, partition them first: names = [a for a in args if isinstance(a, str)]; dtypes = [a for a in args if not isinstance(a, str)], then apply each group separately"],"exampleFix":"# before\nsel = cs.all().exclude(\"foo\", pl.Utf8)\n\n# after\nsel = cs.all().exclude(\"foo\").exclude(pl.Utf8)\n# or\nsel = cs.all() - (cs.by_name(\"foo\") | cs.by_dtype(pl.Utf8))","handlingStrategy":"validation","validationCode":"names = [a for a in args if isinstance(a, str)]\ndtypes = [a for a in args if not isinstance(a, str)]\nassert not (names and dtypes), \"exclude: split names and dtypes into separate calls\"\nsel = base.exclude(*names).exclude(*dtypes)","typeGuard":null,"tryCatchPattern":"try:\n    sel = base.exclude(*args)\nexcept TypeError as e:\n    if \"cannot exclude by both\" in str(e):\n        names = [a for a in args if isinstance(a, str)]\n        dtypes = [a for a in args if not isinstance(a, str)]\n        sel = base.exclude(*names).exclude(*dtypes)\n    else:\n        raise","preventionTips":["Keep name-based and dtype-based exclusions in separate .exclude() calls by convention","In shared helpers, partition args into names/dtypes before calling exclude"],"tags":["polars","selectors","typeerror","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}