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

docs(python): Add examples for bitwise expressions #20503

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading