pola-rs/polars · error
cannot select elements using Sequence with elements of type
Error message
cannot select elements using Sequence with elements of type {qualified_type_name(first)!r} What it means
Polars' Series.__getitem__ (get_series_item_by_key) was given a Python Sequence (list/tuple) whose first element is neither int, str, nor bool-castable numeric, so it cannot be interpreted as Int64 indices. Polars deliberately refuses instead of guessing, raising TypeError after pl.Series(key, dtype=Int64) fails. Only integer sequences (or booleans, handled earlier) are accepted for element selection.
Solutions
- Use all-int lists for row selection: s[[0, 2, 4]].
- Select columns on a DataFrame with all-str lists instead: df[['a', 'b']].
- Cast floats to int explicitly before indexing: s[[int(i) for i in my_indices]].
- Filter None/NaN out of the index list before indexing.
- Use Series.filter() or slice/gather for programmatic element selection.
Example fix
// before s[[0, 1.0, 'x']] // after s[[0, 1]] # or s[s.to_list() ...] after cleaning/casting indices
Defensive patterns
Strategy: type-guard
Validate before calling
def is_int_sequence(key):
return isinstance(key, (list, tuple)) and all(isinstance(x, int) and not isinstance(x, bool) for x in key)
if not is_int_sequence(idx):
idx = [int(x) for x in idx if x is not None]
series[idx] Type guard
def is_int_index_list(key: object) -> bool:
return isinstance(key, (list, tuple)) and len(key) > 0 and all(isinstance(x, int) and not isinstance(x, bool) for x in key) Try / catch
try:
out = series[key]
except TypeError as e:
if 'cannot select elements' in str(e):
out = series[[int(x) for x in key if isinstance(x, (int, float)) and x is not None]]
else:
raise Prevention
- Keep row-index lists purely integer; cast floats with int() before indexing.
- Never mix column names and row indices in one list.
- Filter None/NaN out of programmatically built index lists.
- Prefer s.gather(indices) / s.filter(mask) for explicit selection semantics.
When it happens
Trigger: s[[1, 'a']]; s[('a', 'b')]; s[[None]]; s[[1.5]] passed to Series getitem where the first sequence element fails Series(key, dtype=Int64) construction; also df[some_list] where the list contains non-int, non-str elements routed to series element selection.
Common situations: Mixing column names and row indices in one indexing list; passing float or None values in a list intended as indices; copying pandas-style mixed indexing like df[[0, 'col']]; typos where a float index (1.0) sneaks in from upstream computation.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- can't convert to Decimal
- cannot select elements using key of type
- `arctan2` expected a `str` or `Expr` got a
- `arctan2` expected a `str` or `Expr` got a
- can only call '.item()' if the Series is of length 1, or an…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/c3b483410dd71306.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/getitem.py:76
return _select_elements_by_slice(s, key)
elif isinstance(key, range):
key = range_to_slice(key)
return _select_elements_by_slice(s, key)
elif isinstance(key, Sequence):
if not key:
return s.clear()
first = key[0]
if isinstance(first, bool):
_raise_on_boolean_mask()
try:
indices = pl.Series("", key, dtype=Int64)
except TypeError:
msg = f"cannot select elements using Sequence with elements of type {qualified_type_name(first)!r}"
raise TypeError(msg) from None
indices = _convert_series_to_indices(indices, s.len())
return _select_elements_by_index(s, indices)
elif isinstance(key, pl.Series):
indices = _convert_series_to_indices(key, s.len())
return _select_elements_by_index(s, indices)
elif _check_for_numpy(key) and isinstance(key, np.ndarray):
indices = _convert_np_ndarray_to_indices(key, s.len())
return _select_elements_by_index(s, indices)
msg = f"cannot select elements using key of type {qualified_type_name(key)!r}: {key!r}"
raise TypeError(msg)
def _select_elements_by_slice(s: Series, key: slice) -> Series:
return PolarsSlice(s).apply(key) # type: ignore[return-value]View on GitHub (pinned to fe841f959e)