{"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":"TypeError","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L216-L252","documentation":"When a pl.Series is used as a DataFrame indexing key, polars selects columns by String dtype (names), integer dtypes (positions), or Boolean dtype (mask). Any other Series dtype — Float64, Categorical, temporal — is ambiguous and raises TypeError with the dtype name.","triggerScenarios":"df[pl.Series([0.5, 1.5])]; df[key] where key came from float arithmetic (np.arange(n) / step); df[some_float_column].","commonSituations":"Column indices computed as float ratios (e.g. np.linspace over positions); reusing a data column as a selection key without checking its dtype; indices converted through float stages in a pipeline.","solutions":["Cast positions: df[key.cast(pl.Int64)].","Select by name: df[key.cast(pl.String)] or df.select(key_str).","Generate integer ranges correctly: pl.int_range(...) or np.arange(..., dtype=int)."],"exampleFix":"// before\nidx = pl.Series(np.arange(4) / 2)  # Float64\ncols = df[idx]\n\n// after\ncols = df[idx.cast(pl.Int64))","handlingStrategy":"type-guard","validationCode":"key = pl.Series(key) if not isinstance(key, pl.Series) else key\nif key.dtype == pl.String or key.dtype.is_integer() or key.dtype == pl.Boolean:\n    out = df[key]\nelse:\n    if key.dtype.is_float() and key.cast(pl.Int64).cast(pl.Float64).equals(key):\n        out = df[key.cast(pl.Int64)]\n    else:\n        raise TypeError(f\"Series key dtype {key.dtype} cannot select columns\")","typeGuard":"def is_column_select_series(key: pl.Series) -> bool:\n    return key.dtype == pl.String or key.dtype.is_integer() or key.dtype == pl.Boolean","tryCatchPattern":"try:\n    out = df[key]\nexcept TypeError as e:\n    if \"cannot select columns using Series of type\" in str(e):\n        out = df[key.cast(pl.Int64)] if key.dtype.is_float() else df[key.cast(pl.String)]\n    else:\n        raise","preventionTips":["Generate positional keys with integer dtypes from the start.","Cast computed index Series to Int64 at creation.","Check key.dtype in {String, integer, Boolean} inside generic selection helpers."],"tags":["dataframe","series","dtype","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}