pola-rs/polars · error · TypeError
cannot use Series of dtype
Error message
cannot use Series of dtype {key.dtype!r} for indexing; expected boolean or integer dtype What it means
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.
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`
Example fix
// before s[idx_float_series] = 5 # TypeError // after s[idx_float_series.cast(pl.Int64)] = 5
Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(key, pl.Series) and key.dtype != pl.Boolean and not key.dtype.is_integer():
key = key.cast(pl.Int64)
series[key] = value Type guard
def is_valid_key(key: pl.Series) -> bool:
return key.dtype == pl.Boolean or key.dtype.is_integer() Try / catch
try:
series[key] = value
except TypeError as e:
if "for indexing" in str(e):
series[key.cast(pl.Int64)] = value
else:
raise Prevention
- 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
When it happens
Trigger: `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.
Common situations: Using float position indices from another computation; mistakenly indexing by a string-valued Series; result of a join/computation left as Float64 indices.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- cannot compare datetime.datetime to Series of type
- cannot construct PySeries for type
- cannot convert DataFrame to
- cannot convert List column
- cannot convert Python type
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/edbe72ccf340b626.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/series/series.py:1441
self.scatter(key, value)
return None
elif isinstance(value, Sequence) and not isinstance(value, str):
if self.dtype.is_numeric() or self.dtype.is_temporal():
self.scatter(key, value) # type: ignore[arg-type]
return None
msg = (
f"cannot set Series of dtype: {self.dtype!r} with list/tuple as value;"
" use a scalar value"
)
raise TypeError(msg)
if isinstance(key, Series):
if key.dtype == Boolean:
self._s = self.set(key, value)._s
elif key.dtype.is_integer():
self._s = self.scatter(key, value)._s
else:
msg = f"cannot use Series of dtype {key.dtype!r} for indexing; expected boolean or integer dtype"
raise TypeError(msg)
# TODO: implement for these types without casting to series
elif _check_for_numpy(key) and isinstance(key, np.ndarray):
if key.dtype == np.bool_:
# boolean numpy mask
self._s = self.scatter(np.argwhere(key)[:, 0], value)._s
else:
s = self._from_pyseries(
PySeries.new_u32("", np.array(key, np.uint32), _strict=True)
)
self.__setitem__(s, value)
elif isinstance(key, (list, tuple)):
s = self._from_pyseries(sequence_to_pyseries("", key, dtype=UInt32))
self.__setitem__(s, value)
else:
msg = f'cannot use "{key!r}" for indexing'
raise TypeError(msg)
View on GitHub (pinned to fe841f959e)