Skip to content

Commit

Permalink
docs(python): Add examples for bitwise expressions (#20503)
Browse files Browse the repository at this point in the history
  • Loading branch information
IndexSeek authored Dec 31, 2024
1 parent a569b7d commit 68100f2
Showing 1 changed file with 84 additions and 3 deletions.
87 changes: 84 additions & 3 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10544,15 +10544,96 @@ def bitwise_trailing_zeros(self) -> Expr:
return self._from_pyexpr(self._pyexpr.bitwise_trailing_zeros())

def bitwise_and(self) -> Expr:
"""Perform an aggregation of bitwise ANDs."""
"""Perform an aggregation of bitwise ANDs.
Examples
--------
>>> df = pl.DataFrame({"n": [-1, 0, 1]})
>>> df.select(pl.col("n").bitwise_and())
shape: (1, 1)
┌─────┐
│ n │
│ --- │
│ i64 │
╞═════╡
│ 0 │
└─────┘
>>> df = pl.DataFrame(
... {"grouper": ["a", "a", "a", "b", "b"], "n": [-1, 0, 1, -1, 1]}
... )
>>> df.group_by("grouper").agg(pl.col("n").bitwise_and())
shape: (2, 2)
┌─────────┬─────┐
│ grouper ┆ n │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════╪═════╡
│ b ┆ 1 │
│ a ┆ 0 │
└─────────┴─────┘
"""
return self._from_pyexpr(self._pyexpr.bitwise_and())

def bitwise_or(self) -> Expr:
"""Perform an aggregation of bitwise ORs."""
"""Perform an aggregation of bitwise ORs.
Examples
--------
>>> df = pl.DataFrame({"n": [-1, 0, 1]})
>>> df.select(pl.col("n").bitwise_or())
shape: (1, 1)
┌─────┐
│ n │
│ --- │
│ i64 │
╞═════╡
│ -1 │
└─────┘
>>> df = pl.DataFrame(
... {"grouper": ["a", "a", "a", "b", "b"], "n": [-1, 0, 1, -1, 1]}
... )
>>> df.group_by("grouper").agg(pl.col("n").bitwise_or())
shape: (2, 2)
┌─────────┬─────┐
│ grouper ┆ n │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════╪═════╡
│ a ┆ -1 │
│ b ┆ -1 │
└─────────┴─────┘
"""
return self._from_pyexpr(self._pyexpr.bitwise_or())

def bitwise_xor(self) -> Expr:
"""Perform an aggregation of bitwise XORs."""
"""Perform an aggregation of bitwise XORs.
Examples
--------
>>> df = pl.DataFrame({"n": [-1, 0, 1]})
>>> df.select(pl.col("n").bitwise_xor())
shape: (1, 1)
┌─────┐
│ n │
│ --- │
│ i64 │
╞═════╡
│ -2 │
└─────┘
>>> df = pl.DataFrame(
... {"grouper": ["a", "a", "a", "b", "b"], "n": [-1, 0, 1, -1, 1]}
... )
>>> df.group_by("grouper").agg(pl.col("n").bitwise_xor())
shape: (2, 2)
┌─────────┬─────┐
│ grouper ┆ n │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════════╪═════╡
│ a ┆ -2 │
│ b ┆ -2 │
└─────────┴─────┘
"""
return self._from_pyexpr(self._pyexpr.bitwise_xor())

@deprecate_function(
Expand Down

0 comments on commit 68100f2

Please sign in to comment.