{"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":"TypeError","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L310-L346","documentation":"Row-selector branch of DataFrame.__getitem__ (getitem.py:328). In df[rows, cols] the first slot is parsed by _select_rows, which accepts int, slice, range, Sequence (converted to indices), pl.Series, and np.ndarray; anything else raises this TypeError. For single keys df[k], _select_rows is tried first and its TypeError triggers a fallback to column selection (error 40), so this message mainly escapes from the explicit two-slot form df[rows, cols].","triggerScenarios":"df[{0: 'x'}, :] (dict row key); df[3.0, 'a'] (float index); df[np.int64(2), 'a'] (numpy scalar, not Python int); df[None, 'a']; df[some_object, :].","commonSituations":"Passing floats or numpy scalars produced by computations as the row slot; assuming pandas .loc-style label/dict keys work on polars; mixing up row-first tuple semantics.","solutions":["Normalize scalars: df[int(idx), 'a'] or df[np_val.item(), 'a']","Use explicit methods: df.row(i, named=True) for one row, df.slice(...) for ranges","Pass a list/Series/1D numpy array of ints for multiple rows: df[[0, 2], 'a']"],"exampleFix":"# before\ndf[np.int64(2), \"a\"]   # numpy scalar is not int\n\n# after\ndf[int(np.int64(2)), \"a\"]","handlingStrategy":"type-guard","validationCode":"from collections.abc import Sequence\nimport polars as pl\n\ntry:\n    import numpy as np\n    _ND = (np.ndarray,)\nexcept ImportError:\n    _ND = ()\n\nif not isinstance(row_key, (int, slice, range, Sequence, pl.Series) + _ND):\n    row_key = int(row_key)  # normalize scalars; will raise clearly if impossible\ndf[row_key, \"a\"]","typeGuard":"def is_valid_row_key(key: object) -> bool:\n    import polars as pl\n    from collections.abc import Sequence\n    return isinstance(key, (int, slice, range, Sequence, pl.Series))","tryCatchPattern":null,"preventionTips":["Call .item() or int() on numpy scalars before using them as row indices","Use df.row(i) for single rows to make intent explicit","Remember the tuple is df[rows, cols], not pandas' df[loc, iloc] semantics"],"tags":["python","polars","dataframe","getitem","row-selection","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}