{"record":{"id":"f9eb0a68a93b71c3","repo":"pola-rs/polars","slug":"cannot-select-columns-using-series-of-type-dtype","errorCode":null,"errorMessage":"cannot select columns using Series of type {dtype}","messagePattern":"cannot select columns using Series of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":234,"sourceCode":"        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)\n        elif key.ndim != 1:\n            msg = \"multi-dimensional NumPy arrays not supported as index\"\n            raise TypeError(msg)\n\n        if len(key) == 0:\n            return df.__class__()\n\n        dtype_kind = key.dtype.kind\n        if dtype_kind in (\"i\", \"u\"):\n            return _select_columns_by_index(df, key)\n        elif dtype_kind == \"b\":\n            return _select_columns_by_mask(df, key)\n        elif isinstance(key[0], str):\n            return _select_columns_by_name(df, key)","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L216-L252","documentation":"When the column-selection key is a pl.Series, _select_columns accepts String (names), integer (indices), and Boolean (mask) dtypes only. Any other Series dtype (e.g. Float64, Categorical, List) cannot designate columns, so polars raises TypeError naming the dtype.","triggerScenarios":"df[pl.Series([1.0, 2.0])]; df[cat_series] where cat_series holds column names as Categorical; df[date_series]; passing a float Series of positions without casting to Int64.","commonSituations":"Column names loaded as Categorical from a catalog; float indices from numpy/pandas interop that were never cast; accidental pass-through of a data Series instead of the intended name/index Series.","solutions":["Cast name Series to String: df[names.cast(pl.String)].","Cast positional Series to an integer dtype: df[idx.cast(pl.Int64)].","For boolean masks ensure dtype is Boolean: df[mask.cast(pl.Boolean)].","Convert the Series to a plain list first: df[s.to_list()].","Use df.select(...) with pl.col expressions instead of Series getitem."],"exampleFix":"// before\ndf[pl.Series([0.0, 2.0])]\n// after\ndf[pl.Series([0.0, 2.0]).cast(pl.Int64)]","handlingStrategy":"type-guard","validationCode":"key_s = pl.Series(['a', 'b'])\nassert key_s.dtype in (pl.String, pl.Boolean) or key_s.dtype.is_integer(), f'bad selector dtype {key_s.dtype}'\ndf[key_s]","typeGuard":"def is_column_selector_series(s: 'pl.Series') -> bool:\n    return s.dtype == pl.String or s.dtype == pl.Boolean or s.dtype.is_integer()","tryCatchPattern":"try:\n    out = df[key_series]\nexcept TypeError as e:\n    if 'cannot select columns using Series' in str(e):\n        fixed = (key_series.cast(pl.Int64) if key_series.dtype.is_float()\n                 else key_series.cast(pl.String))\n        out = df[fixed]\n    else:\n        raise","preventionTips":["Cast name-bearing Series (e.g. from Categorical columns) to pl.String before selection.","Cast float position arrays to pl.Int64 before use.","Ensure boolean masks are exactly pl.Boolean, not numeric 0/1.","Prefer df.select() expressions over Series getitem for programmatic selection."],"tags":["polars","dataframe","dtype","column-selection"],"backgroundTag":"unsupported-dtype","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"}