{"record":{"id":"c3b483410dd71306","repo":"pola-rs/polars","slug":"cannot-select-elements-using-sequence-with-element","errorCode":null,"errorMessage":"cannot select elements using Sequence with elements of type {qualified_type_name(first)!r}","messagePattern":"cannot select elements using Sequence with elements of type (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":76,"sourceCode":"        return _select_elements_by_slice(s, key)\n\n    elif isinstance(key, range):\n        key = range_to_slice(key)\n        return _select_elements_by_slice(s, key)\n\n    elif isinstance(key, Sequence):\n        if not key:\n            return s.clear()\n\n        first = key[0]\n        if isinstance(first, bool):\n            _raise_on_boolean_mask()\n\n        try:\n            indices = pl.Series(\"\", key, dtype=Int64)\n        except TypeError:\n            msg = f\"cannot select elements using Sequence with elements of type {qualified_type_name(first)!r}\"\n            raise TypeError(msg) from None\n\n        indices = _convert_series_to_indices(indices, s.len())\n        return _select_elements_by_index(s, indices)\n\n    elif isinstance(key, pl.Series):\n        indices = _convert_series_to_indices(key, s.len())\n        return _select_elements_by_index(s, indices)\n\n    elif _check_for_numpy(key) and isinstance(key, np.ndarray):\n        indices = _convert_np_ndarray_to_indices(key, s.len())\n        return _select_elements_by_index(s, indices)\n\n    msg = f\"cannot select elements using key of type {qualified_type_name(key)!r}: {key!r}\"\n    raise TypeError(msg)\n\n\ndef _select_elements_by_slice(s: Series, key: slice) -> Series:\n    return PolarsSlice(s).apply(key)  # type: ignore[return-value]","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ns[[0, 1.0, 'x']]\n// after\ns[[0, 1]]  # or s[s.to_list() ...] after cleaning/casting indices","handlingStrategy":"type-guard","validationCode":"def is_int_sequence(key):\n    return isinstance(key, (list, tuple)) and all(isinstance(x, int) and not isinstance(x, bool) for x in key)\nif not is_int_sequence(idx):\n    idx = [int(x) for x in idx if x is not None]\nseries[idx]","typeGuard":"def is_int_index_list(key: object) -> bool:\n    return isinstance(key, (list, tuple)) and len(key) > 0 and all(isinstance(x, int) and not isinstance(x, bool) for x in key)","tryCatchPattern":"try:\n    out = series[key]\nexcept TypeError as e:\n    if 'cannot select elements' in str(e):\n        out = series[[int(x) for x in key if isinstance(x, (int, float)) and x is not None]]\n    else:\n        raise","preventionTips":["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."],"tags":["polars","series","indexing","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}