pola-rs/polars · error · TypeError

selecting rows by passing a boolean mask to `__getitem__`…

Error message

selecting rows by passing a boolean mask to `__getitem__` is not supported\n\nHint: Use the `filter` method instead.

What it means

TypeError raised by _raise_on_boolean_mask, a shared helper called from Series/DataFrame __getitem__ and index-conversion paths whenever a boolean mask is supplied where row selection by mask is not allowed. The message directs users to the filter method, which is the supported masking API.

Solutions

  1. Use df.filter(mask) instead of df[mask] for row filtering.

Example fix

df.filter(pl.col('a') > 0)
Defensive patterns

Strategy: validation

When it happens

Trigger: Boolean mask passed to __getitem__ for row selection.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/6041f2c2e3f7fb84. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/getitem.py:457

        else:
            if arr.dtype in (np.int8, np.int16, np.int32):
                arr = arr.astype(np.int64)

        # Update negative indexes to absolute indexes.
        arr = np.where(arr < 0, size + arr, arr)

    # numpy conversion is much faster
    arr = arr.astype(np.uint32) if idx_type == UInt32 else arr.astype(np.uint64)

    return pl.Series("", arr, dtype=idx_type)


def _raise_on_boolean_mask() -> NoReturn:
    msg = (
        "selecting rows by passing a boolean mask to `__getitem__` is not supported"
        "\n\nHint: Use the `filter` method instead."
    )
    raise TypeError(msg)

View on GitHub (pinned to 9b5d73fd00)