{"record":{"id":"b6c27334f231bc69","repo":"pola-rs/polars","slug":"indices-is-not-a-sequence","errorCode":null,"errorMessage":"`indices` is not a sequence","messagePattern":"`indices` is not a sequence","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/interchange/dataframe.py","lineNumber":138,"sourceCode":"        return PolarsColumn(s, allow_copy=self._allow_copy)\n\n    def get_columns(self) -> Iterator[PolarsColumn]:\n        \"\"\"Return an iterator yielding the columns.\"\"\"\n        for column in self._df.get_columns():\n            yield PolarsColumn(column, allow_copy=self._allow_copy)\n\n    def select_columns(self, indices: Sequence[int]) -> PolarsDataFrame:\n        \"\"\"\n        Create a new dataframe by selecting a subset of columns by index.\n\n        Parameters\n        ----------\n        indices\n            Column indices\n        \"\"\"\n        if not isinstance(indices, Sequence):\n            msg = \"`indices` is not a sequence\"\n            raise TypeError(msg)\n        if not isinstance(indices, list):\n            indices = list(indices)\n\n        return PolarsDataFrame(\n            self._df[:, indices],\n            allow_copy=self._allow_copy,\n        )\n\n    def select_columns_by_name(self, names: Sequence[str]) -> PolarsDataFrame:\n        \"\"\"\n        Create a new dataframe by selecting a subset of columns by name.\n\n        Parameters\n        ----------\n        names\n            Column names.\n        \"\"\"\n        if not isinstance(names, Sequence):","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/interchange/dataframe.py#L120-L156","documentation":"Raised by PolarsDataFrame.select_columns when the indices argument is not a collections.abc.Sequence — e.g. a single int, an iterator/generator, a set, or a numpy array. The interchange method indexes columns positionally and requires a genuine Sequence (list, tuple, range, ...).","triggerScenarios":"select_columns(0) with a bare int; select_columns(iter([0, 1])) or a generator; select_columns({0, 2}) (set is not a Sequence); passing a numpy array of indices.","commonSituations":"Consumers deriving column subsets from set operations or generators; wrapping a single index because the caller usually wants one column; feeding numpy integer arrays straight through.","solutions":["Wrap in a list: select_columns(list(indices)) (or [indices] for a single int)","Materialize generators before the call","For numpy arrays, use .tolist()"],"exampleFix":"// before\ndf.__dataframe__().select_columns({0, 2})\n// after\ndf.__dataframe__().select_columns([0, 2])","handlingStrategy":"type-guard","validationCode":"from collections.abc import Sequence\n\nif not isinstance(indices, Sequence):\n    indices = [indices] if isinstance(indices, int) else list(indices)","typeGuard":"from collections.abc import Sequence\n\ndef is_sequence(obj) -> bool:\n    return isinstance(obj, Sequence)","tryCatchPattern":"try:\n    dfi = dfi.select_columns(indices)\nexcept TypeError:\n    dfi = dfi.select_columns(list(indices))","preventionTips":["Materialize sets/generators to lists before interchange calls","Add isinstance(x, Sequence) checks at consumer API boundaries"],"tags":["polars","interchange-protocol","type-validation","sequence"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}