pola-rs/polars · error · TypeError
invalid input for `col` Expected `str` or `DataType`, got
Error message
invalid input for `col`
Expected `str` or `DataType`, got {type(name).__name__!r}. What it means
pl.col validates its first argument: it must be a str (column name) or a polars DataType (dtype selector); other inputs raise TypeError, and notably a list must be passed via pl.col([...]) semantics or multiple names.
Solutions
- Pass a `str` name or a polars dtype like `pl.Int64`
- Use `cs.numeric()` / selectors for dtype-based matching with `import polars.selectors as cs`
- Convert the name with `str(name)` if it is dynamically generated
Example fix
// before
pl.col(42)
// after
pl.col('column_42') # or pl.col(pl.Int64) for dtype selection Defensive patterns
Strategy: type-guard
Validate before calling
if not (isinstance(name, str) or (isinstance(name, type) and is_polars_dtype(name))):
raise TypeError(f'pl.col needs str or DataType, got {type(name).__name__}') Type guard
def is_col_name(n) -> TypeGuard[str]:
return isinstance(n, str) Try / catch
try:
expr = pl.col(name)
except TypeError:
expr = pl.col(str(name)) Prevention
- Annotate name variables as `str`
- Use polars selectors (cs.*) for dtype-based column selection
- Avoid passing pandas dtype objects to pl.col
When it happens
Trigger: Calling `pl.col(42)`, `pl.col(None)`, `pl.col(['a','b'])` (lists go through the iterable branch, but other objects hit this), or passing a third-party type object.
Common situations: Passing a column name built dynamically that ends up as int; confusing pl.col with pl.selectors by passing lists; using a pandas dtype class instead of a polars dtype.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- arr.to_struct() got a str instead of a list. hint: pass
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
- cannot call `map_groups` when grouping by named expressions
- cannot set Series of dtype
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/a61b1602a222ecee.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/functions/col.py:68
"""Create one or more column expressions representing column(s) in a DataFrame."""
dtypes: list[PolarsDataType]
if more_names:
if isinstance(name, str):
names_str = [name]
names_str.extend(more_names) # type: ignore[arg-type]
return pl.Selector._by_name(
names_str, strict=True, expand_patterns=True
).as_expr()
elif is_polars_dtype(name):
dtypes = [name]
dtypes.extend(more_names) # type: ignore[arg-type]
return pl.Selector._by_dtype(dtypes).as_expr() # type: ignore[arg-type]
else:
msg = (
"invalid input for `col`"
f"\n\nExpected `str` or `DataType`, got {type(name).__name__!r}."
)
raise TypeError(msg)
if isinstance(name, str):
return wrap_expr(plr.col(name))
elif is_polars_dtype(name):
dtypes = _polars_dtype_match(name)
return pl.Selector._by_dtype(dtypes).as_expr() # type: ignore[arg-type]
elif isinstance(name, type):
dtypes = _python_dtype_match(name)
return pl.Selector._by_dtype(dtypes).as_expr() # type: ignore[arg-type]
elif isinstance(name, Iterable):
names = list(name)
if not names:
return pl.Selector._by_name(
names=names, # type: ignore[arg-type]
strict=True,
expand_patterns=True,
).as_expr()
View on GitHub (pinned to fe841f959e)