{"record":{"id":"edbe72ccf340b626","repo":"pola-rs/polars","slug":"cannot-use-series-of-dtype-key-dtype-r-for-index","errorCode":null,"errorMessage":"cannot use Series of dtype {key.dtype!r} for indexing; expected boolean or integer dtype","messagePattern":"cannot use Series of dtype (.+?) for indexing; expected boolean or integer dtype","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1441,"sourceCode":"            self.scatter(key, value)\n            return None\n        elif isinstance(value, Sequence) and not isinstance(value, str):\n            if self.dtype.is_numeric() or self.dtype.is_temporal():\n                self.scatter(key, value)  # type: ignore[arg-type]\n                return None\n            msg = (\n                f\"cannot set Series of dtype: {self.dtype!r} with list/tuple as value;\"\n                \" use a scalar value\"\n            )\n            raise TypeError(msg)\n        if isinstance(key, Series):\n            if key.dtype == Boolean:\n                self._s = self.set(key, value)._s\n            elif key.dtype.is_integer():\n                self._s = self.scatter(key, value)._s\n            else:\n                msg = f\"cannot use Series of dtype {key.dtype!r} for indexing; expected boolean or integer dtype\"\n                raise TypeError(msg)\n\n        # TODO: implement for these types without casting to series\n        elif _check_for_numpy(key) and isinstance(key, np.ndarray):\n            if key.dtype == np.bool_:\n                # boolean numpy mask\n                self._s = self.scatter(np.argwhere(key)[:, 0], value)._s\n            else:\n                s = self._from_pyseries(\n                    PySeries.new_u32(\"\", np.array(key, np.uint32), _strict=True)\n                )\n                self.__setitem__(s, value)\n        elif isinstance(key, (list, tuple)):\n            s = self._from_pyseries(sequence_to_pyseries(\"\", key, dtype=UInt32))\n            self.__setitem__(s, value)\n        else:\n            msg = f'cannot use \"{key!r}\" for indexing'\n            raise TypeError(msg)\n","sourceCodeStart":1423,"sourceCodeEnd":1459,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L1423-L1459","documentation":"Series.__setitem__ raises this TypeError when the key is a Series whose dtype is neither Boolean (mask) nor integer (indices). Only boolean masks and integer index Series are valid for set/scatter operations; e.g. a Float64 or Str Series key is rejected.","triggerScenarios":"`series[float_series_or_str_series] = value` — the key Series dtype fails both `key.dtype == Boolean` and `key.dtype.is_integer()` at py-polars/src/polars/series/series.py:1441.","commonSituations":"Using float position indices from another computation; mistakenly indexing by a string-valued Series; result of a join/computation left as Float64 indices.","solutions":["Cast an index Series to integer: `series[key.cast(pl.Int64)] = value`","Convert a float mask to Boolean: `series[(values > 0)] = value` using a boolean expression","If using floats as positions, round/cast them: `series[key.round().cast(pl.Int32)] = value`"],"exampleFix":"// before\ns[idx_float_series] = 5  # TypeError\n// after\ns[idx_float_series.cast(pl.Int64)] = 5","handlingStrategy":"type-guard","validationCode":"if isinstance(key, pl.Series) and key.dtype != pl.Boolean and not key.dtype.is_integer():\n    key = key.cast(pl.Int64)\nseries[key] = value","typeGuard":"def is_valid_key(key: pl.Series) -> bool:\n    return key.dtype == pl.Boolean or key.dtype.is_integer()","tryCatchPattern":"try:\n    series[key] = value\nexcept TypeError as e:\n    if \"for indexing\" in str(e):\n        series[key.cast(pl.Int64)] = value\n    else:\n        raise","preventionTips":["Ensure index Series are Int (or Boolean masks) before __setitem__","Cast float indices explicitly with .cast(pl.Int64)","Build masks from boolean expressions, not raw value Series"],"tags":["polars","python","setitem","indexing","dtype"],"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"}