Skip to content

Commit

Permalink
Merge pull request #30 from condekind/docs/use_broadcast_binary_eleme…
Browse files Browse the repository at this point in the history
…ntwise

Replace binary_elementwise with broadcast_binary_elementwise in chapter 3
  • Loading branch information
MarcoGorelli authored Jul 17, 2024
2 parents 255c664 + 65ad06a commit 8be02d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 3 additions & 4 deletions docs/sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def sum_i64(expr: IntoExpr, other: IntoExpr) -> pl.Expr:

Time to write a binary function, in the sense that it takes two
columns as input and produces a third.
Polars gives us a handy `binary_elementwise` function for computing binary elementwise operations
called `binary_elementwise`.
Polars gives us a handy `broadcast_binary_elementwise` function for computing binary elementwise operations!

Add the following to `src/expressions.rs`:

Expand All @@ -41,7 +40,7 @@ fn sum_i64(inputs: &[Series]) -> PolarsResult<Series> {
let right: &Int64Chunked = inputs[1].i64()?;
// Note: there's a faster way of summing two columns, see
// section 7.
let out: Int64Chunked = binary_elementwise(
let out: Int64Chunked = broadcast_binary_elementwise(
left,
right,
|left: Option<i64>, right: Option<i64>| match (left, right) {
Expand All @@ -54,7 +53,7 @@ fn sum_i64(inputs: &[Series]) -> PolarsResult<Series> {
```
Note that you'll also need to add
```Rust
use polars::prelude::arity::binary_elementwise;
use polars::prelude::arity::broadcast_binary_elementwise;
```
to the top of the `src/expressions.rs` file.

Expand Down
13 changes: 6 additions & 7 deletions src/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::unused_unit)]
use polars::prelude::arity::binary_elementwise;
use polars::prelude::arity::broadcast_binary_elementwise;
use polars::prelude::*;
use pyo3_polars::derive::polars_expr;
use pyo3_polars::export::polars_core::export::num::Signed;
Expand Down Expand Up @@ -59,14 +59,13 @@ fn sum_i64(inputs: &[Series]) -> PolarsResult<Series> {
let right: &Int64Chunked = inputs[1].i64()?;
// Note: there's a faster way of summing two columns, see
// section 7.
let out: Int64Chunked = binary_elementwise(
left,
right,
|left: Option<i64>, right: Option<i64>| match (left, right) {
let out: Int64Chunked =
broadcast_binary_elementwise(left, right, |left: Option<i64>, right: Option<i64>| match (
left, right,
) {
(Some(left), Some(right)) => Some(left + right),
_ => None,
},
);
});
Ok(out.into_series())
}

Expand Down

0 comments on commit 8be02d7

Please sign in to comment.