{"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":"TypeError","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L202-L238","documentation":"DataFrame.__getitem__ dispatches on the first element of a Sequence key: bool → row/column mask, int → column position, str → column name. A first element of any other type (float, None, tuple, nested list) has no selection semantics and raises TypeError naming the type.","triggerScenarios":"df[[1.0, 2.0]]; df[[None, \"a\"]]; df[[(\"a\",)]]; float indices produced by np.linspace or pandas float Index objects.","commonSituations":"Computed column indices arriving as floats (e.g. np.arange(4) * 0.5 rounded); None values from JSON configs mixed into column lists; tuples from multi-index code ported from pandas.","solutions":["Coerce whole-number floats to int: [int(i) for i in key].","Use strings to select by name or ints to select by position — do not mix types.","Filter out None entries before indexing: [k for k in key if k is not None]."],"exampleFix":"// before\ncols = df[np.linspace(0, 3, 3).tolist()]  # floats -> TypeError\n\n// after\ncols = df[[int(round(i)) for i in np.linspace(0, 3, 3)]]","handlingStrategy":"type-guard","validationCode":"def normalize_column_key(key: Sequence):\n    if key and isinstance(key[0], float) and all(k.is_integer() for k in key):\n        return [int(k) for k in key]\n    if not key or not isinstance(key[0], (bool, int, str)):\n        raise TypeError(f\"column key elements must be bool/int/str, got {type(key[0]).__name__}\")\n    return key\n\nout = df[normalize_column_key(key)]","typeGuard":"def is_selectable_column_sequence(key: Sequence) -> bool:\n    return not key or isinstance(key[0], (bool, int, str))","tryCatchPattern":"try:\n    out = df[key]\nexcept TypeError as e:\n    if \"cannot select columns using Sequence\" in str(e):\n        out = df[[int(k) for k in key]]  # if keys are whole numbers\n    else:\n        raise","preventionTips":["Produce int indices with integer arithmetic (np.arange(..., dtype=int)).","Filter None out of column lists from configs before use.","Prefer df.select(...) with explicit names over heterogeneous lists."],"tags":["dataframe","getitem","sequence","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}