pola-rs/polars · error · TypeError

`names` is not a sequence

Error message

`names` is not a sequence

What it means

Raised by PolarsDataFrame.select_columns_by_name when the names argument is not a collections.abc.Sequence — a bare string, generator, set, or iterator. Note a single string is technically a Sequence of characters but usually indicates a caller bug; non-sequence iterables like sets/generators are the hard failures here.

Source

Thrown at py-polars/src/polars/interchange/dataframe.py:158

            indices = list(indices)

        return PolarsDataFrame(
            self._df[:, indices],
            allow_copy=self._allow_copy,
        )

    def select_columns_by_name(self, names: Sequence[str]) -> PolarsDataFrame:
        """
        Create a new dataframe by selecting a subset of columns by name.

        Parameters
        ----------
        names
            Column names.
        """
        if not isinstance(names, Sequence):
            msg = "`names` is not a sequence"
            raise TypeError(msg)

        return PolarsDataFrame(
            self._df.select(names),
            allow_copy=self._allow_copy,
        )

    def get_chunks(self, n_chunks: int | None = None) -> Iterator[PolarsDataFrame]:
        """
        Return an iterator yielding the chunks of the dataframe.

        Parameters
        ----------
        n_chunks
            The number of chunks to return. Must be a multiple of the number of chunks
            in the dataframe. If set to `None` (default), returns all chunks.

        Notes
        -----

View on GitHub (pinned to df599052da)

Solutions

  1. Wrap in a list: select_columns_by_name(list(names)); for a single name use ['a']
  2. Materialize iterators/sets before calling
  3. Validate with isinstance(names, Sequence) at your API boundary

Example fix

// before
df.__dataframe__().select_columns_by_name({'a', 'b'})
// after
df.__dataframe__().select_columns_by_name(['a', 'b'])
Defensive patterns

Strategy: type-guard

Validate before calling

from collections.abc import Sequence

if not isinstance(names, Sequence) or isinstance(names, str):
    names = [names] if isinstance(names, str) else list(names)

Type guard

from collections.abc import Sequence

def is_name_sequence(obj) -> bool:
    return isinstance(obj, Sequence) and not isinstance(obj, str)

Try / catch

try:
    dfi = dfi.select_columns_by_name(names)
except TypeError:
    dfi = dfi.select_columns_by_name(list(names))

Prevention

When it happens

Trigger: select_columns_by_name('a') with a single name; passing a generator of names; passing a set of column names; passing an iterator from map/filter.

Common situations: Consumers computing name subsets via set intersection/difference; single-column convenience paths; piping map(...) results directly into interchange calls.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/66f6e3ab345c124c. Report an issue: GitHub.