{"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":"TypeError","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L345-L381","documentation":"_convert_series_to_indices (getitem.py:363) accepts only integer Series as positional row indices; a Boolean Series deliberately routes to the boolean-mask error, and any other dtype (float, String, temporal, categorical) cannot be interpreted as positions and raises this TypeError naming s.dtype.","triggerScenarios":"df[pl.Series(['a', 'b'])] (attempting label-based row selection); df[pl.Series([0.5, 1.0])] (float indices); s[pl.Series(['x'])] on a Series; passing an uncapped result of np.argmax-like computations held in a float Series.","commonSituations":"Expecting pandas .loc label semantics; using a float column of computed indices without casting; passing a string column of keys instead of using is_in.","solutions":["Cast to integer positions: df[idx.cast(pl.Int64)] (round first if fractional: idx.round(0).cast(pl.Int64))","For value-based row selection use df.filter(pl.col('id').is_in(values)) or a join","For name-based selection of columns, put the string Series in the column slot or use df.select(names)"],"exampleFix":"# before\ndf[pl.Series([1.0, 2.0])]  # float Series cannot be indices\n\n# after\ndf[pl.Series([1.0, 2.0]).cast(pl.Int64)]","handlingStrategy":"type-guard","validationCode":"if not idx.dtype.is_integer():\n    if idx.dtype == pl.Boolean:\n        raise TypeError(\"use df.filter(mask) for boolean row masks\")\n    idx = idx.cast(pl.Int64)\ndf[idx]","typeGuard":"import polars as pl\n\ndef is_index_series(s: pl.Series) -> bool:\n    return s.dtype.is_integer()","tryCatchPattern":null,"preventionTips":["Cast computed float indices to Int64 before passing as row selectors","Use is_in/filter for value-based lookups instead of string Series","Remember polars indexing is positional, not label-based"],"tags":["python","polars","series","row-selection","indices","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}