Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Accept more generic Iterable[bool] in Series.filter #20431

Merged
merged 9 commits into from
Dec 27, 2024
4 changes: 2 additions & 2 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3034,7 +3034,7 @@ def extend(self, other: Series) -> Self:
raise
return self

def filter(self, predicate: Series | list[bool]) -> Self:
def filter(self, predicate: Series | Iterable[bool]) -> Self:
"""
Filter elements by a boolean mask.

Expand All @@ -3060,7 +3060,7 @@ def filter(self, predicate: Series | list[bool]) -> Self:
3
]
"""
if isinstance(predicate, list):
if not isinstance(predicate, Series):
predicate = Series("", predicate)
return self._from_pyseries(self._s.filter(predicate._s))

Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,10 @@ def test_filter() -> None:

assert_series_equal(s.filter(mask), pl.Series("a", [1, 3]))
assert_series_equal(s.filter([True, False, True]), pl.Series("a", [1, 3]))
assert_series_equal(s.filter(np.array([True, False, True])), pl.Series("a", [1, 3]))

with pytest.raises(RuntimeError, match="Expected a boolean mask"):
s.filter(np.array([1, 0, 1]))


def test_gather_every() -> None:
Expand Down
Loading