pola-rs/polars · error · TypeError
invalid input for `exclude` Expected one or more `str` or…
Error message
invalid input for `exclude`
Expected one or more `str` or `DataType`; found {item!r} instead. What it means
The legacy cs.exclude() accepts only column-name strings, Polars DataTypes (including selectors internally), and iterables thereof. Any other item raises this TypeError with a message stating exactly what was found. Note this older exclude differs from the newer Selector.exclude/_combine_as_selector path.
Solutions
- Pass only strings and/or Polars dtypes: cs.exclude('a', pl.UInt32).
- Exclude by expression by switching to the newer API: cs.all().exclude(pl.col('a')) or df.select(cs.all() - cs.by_name('a')).
- Convert indices to names: cs.exclude(*[df.columns[i] for i in idx]).
- Pre-validate items: all(isinstance(x, (str, ...)) for x in items).
Example fix
// before
cs.exclude([pl.col("temp")])
// after
cs.exclude("temp") Defensive patterns
Strategy: validation
Validate before calling
import polars as pl
bad = [x for x in items if not isinstance(x, (str, pl.DataType))]
if bad:
raise ValueError(f"exclude accepts only str/DataType, got: {bad!r}") Type guard
def is_exclude_item(x) -> bool:
import polars as pl
if isinstance(x, (str, pl.DataType)):
return True
return isinstance(x, (list, tuple, set)) and all(isinstance(i, (str, pl.DataType)) for i in x) Try / catch
try:
sel = cs.exclude(*items)
except TypeError as e:
if "invalid input for `exclude`" in str(e):
sel = cs.exclude(*(x for x in items if isinstance(x, str)))
else:
raise Prevention
- Only strings and Polars dtypes belong in legacy exclude(); expressions do not.
- Convert indices to names before excluding.
- Prefer the newer Selector.exclude API which accepts more input kinds.
When it happens
Trigger: cs.exclude(123), cs.exclude([pl.col('a')]) — expressions inside the collection, or None / arbitrary objects passed as items to exclude().
Common situations: Passing column indices instead of names; passing Exprs (allowed in newer code paths via _combine_as_selector but not here); None leaking from optional config.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- cannot parse input into Polars selector
- cannot turn into selector
- cannot turn into selector
- expected a selector; found
- expected one or more `str`, `DataType` or selector; found
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/b01d1921aebc54b7.
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 fe841f959e)