pola-rs/polars · error · TypeError

`indices` is not a sequence

Error message

`indices` is not a sequence

What it means

Type-guard in PolarsDataFrame.select_columns: the interchange protocol requires the column indices to be a Sequence[int]; passing a single int, an iterator, or another non-sequence triggers this TypeError. Wrap the value in a list before calling.

Solutions

  1. Pass indices as a sequence (e.g. a list or range of integers) instead of a scalar or non-sequence value.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at py-polars/src/polars/interchange/dataframe.py:138 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

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

        return PolarsColumn(s, allow_copy=self._allow_copy)

    def get_columns(self) -> Iterator[PolarsColumn]:
        """Return an iterator yielding the columns."""
        for column in self._df.get_columns():
            yield PolarsColumn(column, allow_copy=self._allow_copy)

    def select_columns(self, indices: Sequence[int]) -> PolarsDataFrame:
        """
        Create a new dataframe by selecting a subset of columns by index.

        Parameters
        ----------
        indices
            Column indices
        """
        if not isinstance(indices, Sequence):
            msg = "`indices` is not a sequence"
            raise TypeError(msg)
        if not isinstance(indices, list):
            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):

View on GitHub (pinned to 9b5d73fd00)