pola-rs/polars · error · TypeError

invalid input for `col` Expected iterable of type `str` or…

Error message

invalid input for `col`

Expected iterable of type `str` or `DataType`, got iterable of type {type(item).__name__!r}.

What it means

pl.col with an iterable argument validates each item is str or DataType; an item of any other type raises TypeError naming the iterable's item type, since selectors can only be built from names or dtypes.

Solutions

  1. Ensure every element is a `str` or polars DataType
  2. Filter/coerce the list first, e.g. `[str(x) for x in names]`
  3. Check elements with `isinstance(x, str) or is_polars_dtype(x)` before calling

Example fix

// before
pl.col(['a', 'b', None])
// after
pl.col(['a', 'b'])
Defensive patterns

Strategy: validation

Validate before calling

bad = [x for x in items if not (isinstance(x, str) or is_polars_dtype(x))]
if bad:
    raise TypeError(f'non-str/dtype items in column list: {bad}')

Type guard

def is_valid_col_list(items) -> TypeGuard[list[str]]:
    return all(isinstance(x, str) for x in items)

Try / catch

try:
    expr = pl.col(items)
except TypeError:
    expr = pl.col([x for x in items if x is not None])

Prevention

When it happens

Trigger: Calling `pl.col(['a', 1])` or `pl.col([pl.Int64, 'bad', None])` where at least one element is neither str nor polars dtype.

Common situations: Building column lists dynamically where a None or numeric slipped in; mixing numpy string types (np.str_) or custom wrappers with real strs.

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


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

Appendix: source

Thrown at py-polars/src/polars/functions/col.py:110

                expand_patterns=True,
            ).as_expr()
        elif is_polars_dtype(item):
            dtypes = []
            for nm in names:
                dtypes.extend(_polars_dtype_match(nm))  # type: ignore[arg-type]
            return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]
        elif isinstance(item, type):
            dtypes = []
            for nm in names:
                dtypes.extend(_python_dtype_match(nm))  # type: ignore[arg-type]
            return pl.Selector._by_dtype(dtypes).as_expr()  # type: ignore[arg-type]
        else:
            msg = (
                "invalid input for `col`"
                "\n\nExpected iterable of type `str` or `DataType`,"
                f" got iterable of type {type(item).__name__!r}."
            )
            raise TypeError(msg)
    else:
        msg = (
            "invalid input for `col`"
            f"\n\nExpected `str` or `DataType`, got {type(name).__name__!r}."
        )
        raise TypeError(msg)


if sys.version_info >= (3, 11):
    # note: using `co_qualname` is more robust; can additionally
    # detect class scope from inside classmethods and staticmethods...
    def _get_class_objname(f: FrameType) -> str:
        return f.f_code.co_qualname.split(".")[-2:][0]

    _have_qualname = True
else:
    # ... but it's not available until 3.11
    def _get_class_objname(f: FrameType) -> str:

View on GitHub (pinned to fe841f959e)