{"record":{"id":"259c66ee6192039f","repo":"pola-rs/polars","slug":"cannot-treat-series-of-type-s-dtype-as-indices","errorCode":null,"errorMessage":"cannot treat Series of type {s.dtype} as indices","messagePattern":"cannot treat Series of type (.+?) as indices","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":363,"sourceCode":"    #   - pl.UInt32 (polars) or pl.UInt64 (polars_u64_idx) Series indexes.\n    #   - Other unsigned Series indexes are converted to pl.UInt32 (polars)\n    #     or pl.UInt64 (polars_u64_idx).\n    #   - Signed Series indexes are converted pl.UInt32 (polars) or\n    #     pl.UInt64 (polars_u64_idx) after negative indexes are converted\n    #     to absolute indexes.\n\n    # pl.UInt32 (polars) or pl.UInt64 (polars_u64_idx).\n    idx_type = get_index_type()\n\n    if s.dtype == idx_type:\n        return s\n\n    if not s.dtype.is_integer():\n        if s.dtype == Boolean:\n            _raise_on_boolean_mask()\n        else:\n            msg = f\"cannot treat Series of type {s.dtype} as indices\"\n            raise TypeError(msg)\n\n    if s.len() == 0:\n        return pl.Series(s.name, [], dtype=idx_type)\n\n    if idx_type == UInt32:\n        if s.dtype in {Int64, UInt64} and s.max() >= U32_MAX:  # type: ignore[operator]\n            msg = \"index positions should be smaller than 2^32\"\n            raise ValueError(msg)\n        if s.dtype == Int64 and s.min() < -U32_MAX:  # type: ignore[operator]\n            msg = \"index positions should be greater than or equal to -2^32\"\n            raise ValueError(msg)\n\n    if s.dtype.is_signed_integer():\n        if s.min() < 0:  # type: ignore[operator]\n            if idx_type == UInt32:\n                idxs = s.cast(Int32) if s.dtype in {Int8, Int16} else s\n            else:\n                idxs = s.cast(Int64) if s.dtype in {Int8, Int16, Int32} else s","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L345-L381","documentation":"Raised by `_convert_series_to_indices` when a Series used as a row-index in `__getitem__`/`_select_rows` has a dtype that is neither an integer nor a boolean (e.g. Float64, Utf8/str, Categorical). Polars only accepts integer-position Series as row selectors; anything else is ambiguous, so it fails fast with a TypeError.","triggerScenarios":"Calling `s[some_series]` or `df[series]` where `some_series` contains floats or strings instead of integer positions, e.g. `s[pl.Series([0.0, 1.0])]` or indexing a DataFrame with a Series of column names where rows are expected.","commonSituations":"Building index Series from arithmetic that promotes ints to floats (`pl.int_range(...) / 2`), receiving index data from JSON/CSV parsed as floats or strings, or accidentally swapping row-index and column-name selectors.","solutions":["Cast the Series to an integer dtype before indexing: `s[pl.Series([0.0, 1.0]).cast(pl.UInt32)]`.","If the values are labels not positions, use a filter/join or `df.with_row_index()` to convert labels to positions first.","If a boolean selector was intended, pass the boolean Series to `.filter()` instead of `__getitem__`."],"exampleFix":"// before\nidx = (pl.int_range(0, 10) / 2).unique()\nsub = s[idx]  # TypeError: cannot treat Series of type f64 as indices\n// after\nidx = (pl.int_range(0, 10) // 2).unique()\nsub = s[idx]","handlingStrategy":"validation","validationCode":"if isinstance(idx, pl.Series) and not idx.dtype.is_integer():\n    if idx.dtype == pl.Boolean:\n        raise ValueError(\"use .filter(mask) instead of boolean masking via __getitem__\")\n    idx = idx.cast(pl.UInt32)","typeGuard":"def is_int_series(x) -> bool:\n    return isinstance(x, pl.Series) and x.dtype.is_integer()","tryCatchPattern":"try:\n    sub = s[idx_series]\nexcept TypeError as e:\n    if \"as indices\" in str(e):\n        sub = s.filter(idx_series) if idx_series.dtype == pl.Boolean else s[idx_series.cast(pl.UInt32)]\n    else:\n        raise","preventionTips":["Always build index Series with integer ops (// not /).","Validate `idx.dtype.is_integer()` before `__getitem__`.","Route boolean masks to `.filter()` by habit."],"tags":["polars","indexing","dtype","typeerror"],"backgroundTag":"type-mismatch","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"}