{"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":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":582,"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":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L564-L600","documentation":"cs.exclude() cannot mix column names and dtypes in a single call. If both the string-collection and dtype-collection are non-empty after parsing, this TypeError is raised because the resulting exclusion semantics would be ambiguous.","triggerScenarios":"cs.exclude('a', pl.Int64) — one call passing both a column name and a dtype; combining user-supplied names and dtypes from separate config sources into one exclude call.","commonSituations":"Config files that provide both 'exclude_columns' and 'exclude_dtypes' which get splatted into one call; gradual refactors that merged two exclude calls.","solutions":["Split into two calls: cs.exclude('a') | cs.exclude(pl.Int64) — union the resulting selectors.","Convert names to a dtype-neutral form, e.g. exclude everything by name only: cs.exclude('a', 'b').","Use the modern API: cs.all() - cs.by_name('a') - cs.by_dtype(pl.Int64).","In calling code, assert not (names and dtypes) before building the exclusion."],"exampleFix":"// before\ncs.exclude(\"id\", pl.Float64)\n// after\ncs.exclude(\"id\") | cs.exclude(pl.Float64)","handlingStrategy":"validation","validationCode":"names, dtypes = split_names_and_dtypes(items)\nif names and dtypes:\n    raise ValueError(\"cannot exclude by both name and dtype in one call; split the call\")","typeGuard":"def can_exclusion_combine(names: list, dtypes: list) -> bool:\n    return not (names and dtypes)","tryCatchPattern":"try:\n    sel = cs.exclude(*items)\nexcept TypeError as e:\n    if \"cannot exclude by both\" in str(e):\n        sel = cs.exclude(*names) | cs.exclude(*dtypes)\n    else:\n        raise","preventionTips":["Keep column-name exclusions and dtype exclusions in separate calls, then union with |.","Validate merged config inputs before splatting them into one exclude call.","Consider cs.all() - cs.by_name(...) - cs.by_dtype(...) for complex exclusions."],"tags":["polars","selectors","conflicting-options"],"backgroundTag":"mutually-exclusive-options","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"}