{"record":{"id":"1bdb38e5dc5cff5e","repo":"pola-rs/polars","slug":"cannot-select-columns-using-sequence-with-elements","errorCode":null,"errorMessage":"cannot select columns using Sequence with elements of type {qualified_type_name(first)!r}","messagePattern":"cannot select columns using Sequence with elements of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":220,"sourceCode":"        rng = range(df.width)[int_slice]\n        return _select_columns_by_index(df, rng)\n\n    elif isinstance(key, range):\n        return _select_columns_by_index(df, key)\n\n    elif isinstance(key, Sequence):\n        if not key:\n            return df.__class__()\n        first = key[0]\n        if isinstance(first, bool):\n            return _select_columns_by_mask(df, key)  # type: ignore[arg-type]\n        elif isinstance(first, int):\n            return _select_columns_by_index(df, key)  # type: ignore[arg-type]\n        elif isinstance(first, str):\n            return _select_columns_by_name(df, key)  # type: ignore[arg-type]\n        else:\n            msg = f\"cannot select columns using Sequence with elements of type {qualified_type_name(first)!r}\"\n            raise TypeError(msg)\n\n    elif isinstance(key, pl.Series):\n        if key.is_empty():\n            return df.__class__()\n        dtype = key.dtype\n        if dtype == String:\n            return _select_columns_by_name(df, key)\n        elif dtype.is_integer():\n            return _select_columns_by_index(df, key)\n        elif dtype == Boolean:\n            return _select_columns_by_mask(df, key)\n        else:\n            msg = f\"cannot select columns using Series of type {dtype}\"\n            raise TypeError(msg)\n\n    elif _check_for_numpy(key) and isinstance(key, np.ndarray):\n        if key.ndim == 0:\n            key = np.atleast_1d(key)","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L202-L238","documentation":"_select_columns handles DataFrame column selection via getitem. A Sequence key is accepted only when every element is a str (name) or int (column index). If the first element is any other type, polars cannot tell whether the sequence means names or indices and raises TypeError naming the element type.","triggerScenarios":"df[['a', 0]] (mixed str/int); df[[None]]; df[[1.0]]; df[[('a',)]] passed to DataFrame getitem for column selection.","commonSituations":"Mixing column names with positional indices copied from pandas code; list comprehension producing floats; passing a column list containing None placeholders; building selectors programmatically where a numeric id leaked into a name list.","solutions":["Use a consistent all-str list for names: df[['a', 'b']].","Or a consistent all-int list for positions: df[[0, 2]].","Use df.select(...) with expressions for mixed/programmatic selection.","Cast numeric entries to str if they are actually names, or to int if positional.","Remove None or non-str placeholders from the column list first."],"exampleFix":"// before\ndf[['name', 0]]\n// after\ndf[['name', 'age']]  # or df.select(pl.col('name'), pl.col(0) if positional via df[:, [0]])","handlingStrategy":"validation","validationCode":"cols = ['a', 'b']\nassert all(isinstance(c, str) for c in cols) or all(isinstance(c, int) for c in cols), 'mixed column selector'\ndf[cols]","typeGuard":"def is_valid_column_list(key: object) -> bool:\n    return (isinstance(key, (list, tuple)) and len(key) > 0 and\n            (all(isinstance(x, str) for x in key) or all(isinstance(x, int) for x in key)))","tryCatchPattern":"try:\n    out = df[cols]\nexcept TypeError as e:\n    if 'cannot select columns using Sequence' in str(e):\n        out = df.select([str(c) for c in cols])\n    else:\n        raise","preventionTips":["Standardize column selectors as lists of str; convert positional ints at the boundary.","Validate programmatically built column lists for None/float leakage.","Use df.select(pl.col(...)) for anything dynamic or mixed.","Type-annotate column lists as list[str] so linters catch mixed types."],"tags":["polars","dataframe","column-selection","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}