{"record":{"id":"f9024d7bcf5b62d8","repo":"pola-rs/polars","slug":"cannot-select-rows-using-key-of-type-qualified-ty","errorCode":null,"errorMessage":"cannot select rows using key of type {qualified_type_name(key)!r}: {key!r}","messagePattern":"cannot select rows using key of type (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":328,"sourceCode":"        if not key:\n            return df.clear()\n        if isinstance(key[0], bool):\n            _raise_on_boolean_mask()\n        s = pl.Series(\"\", key, dtype=Int64)\n        indices = _convert_series_to_indices(s, df.height)\n        return _select_rows_by_index(df, indices)\n\n    elif isinstance(key, pl.Series):\n        indices = _convert_series_to_indices(key, df.height)\n        return _select_rows_by_index(df, indices)\n\n    elif _check_for_numpy(key) and isinstance(key, np.ndarray):\n        indices = _convert_np_ndarray_to_indices(key, df.height)\n        return _select_rows_by_index(df, indices)\n\n    else:\n        msg = f\"cannot select rows using key of type {qualified_type_name(key)!r}: {key!r}\"\n        raise TypeError(msg)\n\n\ndef _select_rows_by_slice(df: DataFrame, key: slice) -> DataFrame:\n    return PolarsSlice(df).apply(key)  # type: ignore[return-value]\n\n\ndef _select_rows_by_index(df: DataFrame, key: Series) -> DataFrame:\n    return df._from_pydf(df._df.gather_with_series(key._s))\n\n\n# UTILS\n\n\ndef _convert_series_to_indices(s: Series, size: int) -> Series:\n    \"\"\"Convert a Series to indices, taking into account negative values.\"\"\"\n    # Unsigned or signed Series (ordered from fastest to slowest).\n    #   - pl.UInt32 (polars) or pl.UInt64 (polars_u64_idx) Series indexes.\n    #   - Other unsigned Series indexes are converted to pl.UInt32 (polars)","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L310-L346","documentation":"Raised by Polars when a row-selection key is of a type that is not an int, slice, range, sequence of ints, or a (NumPy-checkable) ndarray — the fallback TypeError in _select_rows. Any other key type cannot be interpreted as a row selector.","triggerScenarios":"df[\"row_label\"] (string key goes to column selection normally, but in a row context / via getitem helpers), df[1.5] (float), df[{0}] (set), passing a custom sequence-like object not recognized as a sequence of ints, df[np.array([[0,1]])] 2-D arrays etc. depending on conversion failure.","commonSituations":"Copy-pasted pandas code using label-based indexing where the label is a non-int type; passing a float position like df[2.0]; passing a set of row positions; accidentally calling row selection with a dict.","solutions":["Use an int, slice, range, list/tuple/ndarray of ints, or a boolean mask to select rows: df[0], df[1:5], df[[0, 2]]","Cast floats to int: df[int(2.0)] or convert sets to lists: df[sorted(key)]","For label or condition-based selection use df.filter(expr) instead of positional keys"],"exampleFix":"// before\ndf[2.0]            # float key -> TypeError\n// after\ndf[2]              # int\ndf.filter(pl.col(\"id\") == 2)  # by value","handlingStrategy":"type-guard","validationCode":"if not isinstance(key, (int, slice, range)) and not (\n    isinstance(key, (list, tuple)) and all(isinstance(v, int) for v in key)\n):\n    raise TypeError(f\"unsupported row key: {type(key)}\")","typeGuard":"def is_valid_row_key(key) -> bool:\n    import polars as pl\n    if isinstance(key, (int, slice, range, pl.Series)):\n        return True\n    if isinstance(key, (list, tuple)):\n        return all(isinstance(v, int) and not isinstance(v, bool) for v in key)\n    try:\n        import numpy as np\n        return isinstance(key, np.ndarray) and key.dtype.kind in (\"i\", \"u\", \"b\")\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    out = df[key]\nexcept TypeError as e:\n    if \"cannot select rows\" in str(e):\n        out = df.filter(pl.col(\"id\") == key)  # fall back to value-based selection\n    else:\n        raise","preventionTips":["Cast float positions to int before row indexing","Do not use sets, dicts, or label strings as row keys; use filter() instead","Prefer df.filter(df[\"id\"] == value) over positional lookups when labels are meant"],"tags":["polars","dataframe","type-error","row-selection"],"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"}