{"record":{"id":"6041f2c2e3f7fb84","repo":"pola-rs/polars","slug":"selecting-rows-by-passing-a-boolean-mask-to-get","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/getitem.py#L439-L458","documentation":"polars deliberately does not support boolean row masks in __getitem__: a bool Sequence, bool pl.Series, or bool numpy array used as a row key routes to _raise_on_boolean_mask (getitem.py:457), which points to filter. Column masks in the second slot (df[:, bool_mask]) are supported (see error 41); this error is only about the row slot.","triggerScenarios":"df[df['x'] > 0]; df[[True, False, True]]; df[np.array([True, False])]; df[bool_series] returned by an expression; s[bool_mask] on a Series.","commonSituations":"Muscle memory from pandas/NumPy boolean indexing; ported notebooks; condition Series produced by pl.col comparisons then passed to [].","solutions":["Use DataFrame.filter: df.filter(pl.col('x') > 0) or df.filter(bool_series)","For Series: s.filter(s > 0)","If positions are truly needed: idx = np.flatnonzero(mask.to_numpy()); df[idx]"],"exampleFix":"# before\ndf[df[\"x\"] > 0]\n\n# after\ndf.filter(pl.col(\"x\") > 0)","handlingStrategy":"fallback","validationCode":"def select_rows(frame, key):\n    if isinstance(key, pl.Series) and key.dtype == pl.Boolean:\n        return frame.filter(key)          # supported path\n    if isinstance(key, list) and key and isinstance(key[0], bool):\n        return frame.filter(pl.Series(key))\n    return frame[key]","typeGuard":"import polars as pl\n\ndef is_boolean_mask(key) -> bool:\n    if isinstance(key, pl.Series):\n        return key.dtype == pl.Boolean\n    if isinstance(key, (list, tuple)):\n        return bool(key) and isinstance(key[0], bool)\n    return False","tryCatchPattern":"try:\n    out = df[mask]\nexcept TypeError as exc:\n    if \"boolean mask\" in str(exc):\n        out = df.filter(mask)\n    else:\n        raise","preventionTips":["Adopt filter as the default row-selection verb; reserve [] for slices and integer positions","Search ported pandas code for df[...] with boolean expressions","Column masks are legal only in df[:, mask]; keep that distinction in review checklists"],"tags":["python","polars","boolean-mask","filter","pandas-migration","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}