Skip to content

Commit

Permalink
python fix horizontal all any (#2820)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 3, 2022
1 parent 9cb69b2 commit 5a013c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,9 @@ def any(name: Union[str, List["pli.Expr"]]) -> "pli.Expr":
Evaluate columnwise or elementwise with a bitwise OR operation.
"""
if isinstance(name, list):
return fold(lit(0), lambda a, b: a | b, name).alias("any")
return fold(lit(False), lambda a, b: a.cast(bool) | b.cast(bool), name).alias(
"any"
)
return col(name).any()


Expand Down Expand Up @@ -905,7 +907,9 @@ def all(name: Optional[Union[str, List["pli.Expr"]]] = None) -> "pli.Expr":
if name is None:
return col("*")
if isinstance(name, list):
return fold(lit(0), lambda a, b: a & b, name).alias("all")
return fold(lit(True), lambda a, b: a.cast(bool) & b.cast(bool), name).alias(
"all"
)
return col(name).all()


Expand Down
27 changes: 27 additions & 0 deletions py-polars/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,30 @@ def test_concat_horizontal() -> None:
}
)
assert out.frame_equal(expected)


def test_all_any_horizontally() -> None:
df = pl.DataFrame(
[
[False, False, True],
[False, False, True],
[True, False, False],
[False, None, True],
[None, None, False],
],
columns=["var1", "var2", "var3"],
)

expected = pl.DataFrame(
{
"any": [True, True, False, True, None],
"all": [False, False, False, None, False],
}
)

assert df.select(
[
pl.any([pl.col("var2"), pl.col("var3")]),
pl.all([pl.col("var2"), pl.col("var3")]),
]
).frame_equal(expected)

0 comments on commit 5a013c6

Please sign in to comment.