pola-rs/polars · error · TypeError

cannot exclude by both column name and dtype

Error message

cannot exclude by both column name and dtype

What it means

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.

Solutions

  1. Split into two calls: cs.exclude('a') | cs.exclude(pl.Int64) — union the resulting selectors.
  2. Convert names to a dtype-neutral form, e.g. exclude everything by name only: cs.exclude('a', 'b').
  3. Use the modern API: cs.all() - cs.by_name('a') - cs.by_dtype(pl.Int64).
  4. In calling code, assert not (names and dtypes) before building the exclusion.

Example fix

// before
cs.exclude("id", pl.Float64)
// after
cs.exclude("id") | cs.exclude(pl.Float64)
Defensive patterns

Strategy: validation

Validate before calling

names, dtypes = split_names_and_dtypes(items)
if names and dtypes:
    raise ValueError("cannot exclude by both name and dtype in one call; split the call")

Type guard

def can_exclusion_combine(names: list, dtypes: list) -> bool:
    return not (names and dtypes)

Try / catch

try:
    sel = cs.exclude(*items)
except TypeError as e:
    if "cannot exclude by both" in str(e):
        sel = cs.exclude(*names) | cs.exclude(*dtypes)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Config files that provide both 'exclude_columns' and 'exclude_dtypes' which get splatted into one call; gradual refactors that merged two exclude calls.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/2d4cd8a5c3df5f80. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/selectors.py:582

                if isinstance(columns, Collection) and not isinstance(columns, str)
                else [columns]
            ),
            *more_columns,
        ):
            if isinstance(item, str):
                exclude_cols.append(item)
            elif is_polars_dtype(item):
                exclude_dtypes.append(item)
            else:
                msg = (
                    "invalid input for `exclude`"
                    f"\n\nExpected one or more `str` or `DataType`; found {item!r} instead."
                )
                raise TypeError(msg)

        if exclude_cols and exclude_dtypes:
            msg = "cannot exclude by both column name and dtype"
            raise TypeError(msg)

        excluded = (
            by_dtype(exclude_dtypes)
            if exclude_dtypes
            else Selector._by_name(
                exclude_cols,
                strict=False,
                expand_patterns=True,
            )
        )
        return self - excluded

    def as_expr(self) -> Expr:
        """
        Materialize the `selector` as a normal expression.

        This ensures that the operators `|`, `&`, `~` and `-`
        are applied on the data and not on the selector sets.

View on GitHub (pinned to fe841f959e)