Skip to content

Commit

Permalink
Merge pull request #10 from thomasjpfan/use_binary_elementwise_values
Browse files Browse the repository at this point in the history
Use binary_elementwise_values in list tutorial
  • Loading branch information
MarcoGorelli authored Jan 28, 2024
2 parents 8c50a8b + f8494db commit 2cbc79d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions docs/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,10 @@ fn weighted_mean(inputs: &[Series]) -> PolarsResult<Series> {
|values_inner: &Series, weights_inner: &Series| -> Option<f64> {
let values_inner = values_inner.i64().unwrap();
let weights_inner = weights_inner.f64().unwrap();
let out_inner: Float64Chunked = binary_elementwise(
let out_inner: Float64Chunked = binary_elementwise_values(
values_inner,
weights_inner,
|opt_value: Option<i64>, opt_weight: Option<f64>| match (opt_value, opt_weight) {
(Some(value), Some(weight)) => Some(value as f64 * weight),
_ => None,
},
|value: i64, weight: f64| value as f64 * weight,
);
match (out_inner.sum(), weights_inner.sum()) {
(Some(weighted_sum), Some(weights_sum)) => Some(weighted_sum / weights_sum),
Expand All @@ -122,8 +119,14 @@ fn weighted_mean(inputs: &[Series]) -> PolarsResult<Series> {
Ok(out.into_series())
}
```
That's it! This version only accepts `Int64` values - see section 2 for how you could make it more
generic.
Make sure to also add

```rust
use polars::prelude::arity::binary_elementwise_values;
```

to the top of the file. That's it! This version only accepts `Int64` values - see section 2 for
how you could make it more generic.

To try it out, we compile with `maturin develop` (or `maturin develop --release` if you're
benchmarking), and then we should be able to run `run.py`:
Expand Down

0 comments on commit 2cbc79d

Please sign in to comment.