{"record":{"id":"531a5c8280fdc5b3","repo":"pola-rs/polars","slug":"selecting-rows-by-passing-a-boolean-mask-to-getitem-is-not","errorCode":null,"errorMessage":"selecting rows by passing a boolean mask to `__getitem__` is not supported\n\nHint: Use the `filter` method instead.","messagePattern":"selecting rows by passing a boolean mask to `__getitem__` is not supported\n\nHint: Use the `filter` method instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":457,"sourceCode":"        else:\n            if arr.dtype in (np.int8, np.int16, np.int32):\n                arr = arr.astype(np.int64)\n\n        # Update negative indexes to absolute indexes.\n        arr = np.where(arr < 0, size + arr, arr)\n\n    # numpy conversion is much faster\n    arr = arr.astype(np.uint32) if idx_type == UInt32 else arr.astype(np.uint64)\n\n    return pl.Series(\"\", arr, dtype=idx_type)\n\n\ndef _raise_on_boolean_mask() -> NoReturn:\n    msg = (\n        \"selecting rows by passing a boolean mask to `__getitem__` is not supported\"\n        \"\\n\\nHint: Use the `filter` method instead.\"\n    )\n    raise TypeError(msg)\n","sourceCodeStart":439,"sourceCodeEnd":458,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L439-L458","documentation":"Polars deliberately removed support for boolean-mask row selection through `__getitem__`. `_raise_on_boolean_mask` raises a TypeError directing users to the `filter` method, which is the supported (and faster/lazier-optimizable) API for mask-based row selection.","triggerScenarios":"`s[mask]` or `df[mask]` where mask is a boolean Series or NumPy bool array, e.g. `df[df['a'] > 5]`, `s[pl.Series([True, False, True])]`.","commonSituations":"Pandas-style habits: `df[df.x > 0]` works in pandas but not polars `__getitem__`; migrating pandas notebooks; quick REPL filtering.","solutions":["Use `df.filter(pl.col('a') > 5)` for DataFrames.","Use `s.filter(mask)` for Series.","For NumPy bool arrays, convert: `s.filter(pl.Series(mask))`."],"exampleFix":"// before\nout = df[df['a'] > 5]  # TypeError\n// after\nout = df.filter(pl.col('a') > 5)","handlingStrategy":"fallback","validationCode":"if isinstance(mask, (pl.Series, np.ndarray)) and mask.dtype in (pl.Boolean, bool, np.bool_):\n    return s.filter(pl.Series(mask))","typeGuard":"def is_boolean_mask(x) -> bool:\n    return (isinstance(x, pl.Series) and x.dtype == pl.Boolean) or (isinstance(x, np.ndarray) and x.dtype == np.bool_)","tryCatchPattern":"try:\n    out = df[mask]\nexcept TypeError as e:\n    if \"boolean mask\" in str(e):\n        out = df.filter(pl.Series(mask))\n    else:\n        raise","preventionTips":["Replace pandas-style `df[mask]` with `df.filter(cond)` when porting code.","Grep codebases for `df[df.` patterns during pandas->polars migration.","Use `pl.col(...) > x` expressions directly inside `.filter()`."],"tags":["polars","boolean-mask","filter","pandas-migration"],"backgroundTag":"unsupported-operation","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"}