pola-rs/polars · error · TypeError

invalid input for `exclude`\n\nExpected one or more `str`…

Error message

invalid input for `exclude`\n\nExpected one or more `str` or `DataType`; found {item!r} instead.

What it means

selectors.exclude() validates every item in `columns`/`more_columns`: strings go to exclude_cols and polars dtypes to exclude_dtypes. This generic TypeError guard fires for any other input type (e.g. an int, a function, or a nested non-string value) passed to exclude(), telling you exactly which offending item was rejected.

Solutions

  1. Pass str names or DataType instances to exclude; other types are invalid.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at py-polars/src/polars/selectors.py:586 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/a0073647bc67d57c. Report an issue: GitHub.

Appendix: source

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

        exclude_dtypes: builtins.list[PolarsDataType] = []
        for item in (
            *(
                columns
                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:
        """

View on GitHub (pinned to 68506541d2)