{"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":"TypeError","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L58-L94","documentation":"Series.__getitem__ accepts slices, int, Sequences of integers, pl.Series, and NumPy arrays. For a Sequence, polars builds pl.Series(\"\", key, dtype=Int64); if the first element's type makes that fail (strings, floats, None, mixed), the TypeError is re-raised with this message naming the offending element type.","triggerScenarios":"s[[\"a\", \"b\"]] (name-based indexing is not supported on Series); s[[0.0, 1.0]]; s[[None, 1]]; indices decoded from JSON as strings or floats.","commonSituations":"Reusing DataFrame-style df[[\"a\",\"b\"]] syntax on a Series; index lists loaded from JSON/config arriving as strings or floats; numpy operations returning float indices.","solutions":["Use integer indices for positional selection: s[[0, 1]].","Coerce numeric keys: [int(i) for i in key] when they are whole numbers.","For name/value-based selection on a Series, use a Boolean mask: s[s.is_in([\"a\", \"b\"])]."],"exampleFix":"// before\nrows = s[[\"a\", \"b\"]]  # Series, not DataFrame\n\n// after\nrows = s[s.is_in([\"a\", \"b\"])]  # boolean mask by value","handlingStrategy":"type-guard","validationCode":"if isinstance(key, (list, tuple)) and key and not all(isinstance(i, int) and not isinstance(i, bool) for i in key):\n    if all(isinstance(i, float) and i.is_integer() for i in key):\n        key = [int(i) for i in key]\n    else:\n        raise TypeError(f\"Series indexing needs int indices, got {type(key[0]).__name__}\")\nrows = s[list(key)]","typeGuard":"def is_int_index_sequence(key: Sequence) -> bool:\n    return bool(key) and all(\n        isinstance(i, int) and not isinstance(i, bool) for i in key\n    )","tryCatchPattern":"try:\n    rows = s[key]\nexcept TypeError as e:\n    if \"cannot select elements using Sequence\" in str(e):\n        rows = s[s.is_in(list(key))]  # fall back to value-based mask\n    else:\n        raise","preventionTips":["Use s[s.is_in([...])] for value-based lookups on Series.","Coerce JSON-loaded indices to int before indexing.","Remember Series indexing is positional/int only; names select only on DataFrame."],"tags":["series","getitem","sequence","indexing"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}