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

feat: Add rolling_(min/max/sum), rolling_(min/max/sum)_by for pl.Boolean #19112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

EvanLai88
Copy link

On version 1.9.0, rolling_(min/max/sum), rolling_(min/max/sum)_by operations are not implemented for dtype pl.Boolean.

Before:

>>> import polars as pl
>>> pl.DataFrame(
...     {"a": [-10, 34, 20, -30, -20, -45, 30]},
... ).with_columns(
...     true_cnt_in_2_win=pl.col("a").gt(0).rolling_sum(2),
...     all_true_in_2_win=pl.col("a").gt(0).rolling_min(2),
...     any_true_in_2_win=pl.col("a").gt(0).rolling_max(2),
... )
thread 'polars-0' panicked at /home/runner/work/polars/polars/crates/polars-time/src/chunkedarray/rolling_window/dispatch.rs:209:9:
not implemented for dtype Boolean
thread 'polars-1' panicked at /home/runner/work/polars/polars/crates/polars-time/src/chunkedarray/rolling_window/dispatch.rs:281:9:
not implemented for dtype Boolean
thread 'polars-2' panicked at /home/runner/work/polars/polars/crates/polars-time/src/chunkedarray/rolling_window/dispatch.rs:319:9:
not implemented for dtype Boolean

After:

>>> import polars as pl
>>> pl.DataFrame(
...     {"a": [-10, 34, 20, -30, -20, -45, 30]},
... ).with_columns(
...     true_cnt_in_2_win=pl.col("a").gt(0).rolling_sum(2),
...     all_true_in_2_win=pl.col("a").gt(0).rolling_min(2),
...     any_true_in_2_win=pl.col("a").gt(0).rolling_max(2),
... )
shape: (7, 4)
┌─────┬───────────────────┬───────────────────┬───────────────────┐
│ atrue_cnt_in_2_winall_true_in_2_winany_true_in_2_win │
│ ------------               │
│ i64u32boolbool              │
╞═════╪═══════════════════╪═══════════════════╪═══════════════════╡
│ -10nullnullnull              │
│ 341falsetrue              │
│ 202truetrue              │
│ -301falsetrue              │
│ -200falsefalse             │
│ -450falsefalse             │
│ 301falsetrue              │
└─────┴───────────────────┴───────────────────┴───────────────────┘

Note:
Other rolling_* operations, such as rolling_mean, already support the pl.Boolean dtype via to_float(), if I’m not mistaken.

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Oct 6, 2024
Copy link

codecov bot commented Oct 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.65%. Comparing base (194b31e) to head (46cf22c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19112   +/-   ##
=======================================
  Coverage   79.65%   79.65%           
=======================================
  Files        1531     1531           
  Lines      208937   208956   +19     
  Branches     2418     2418           
=======================================
+ Hits       166421   166442   +21     
+ Misses      41969    41967    -2     
  Partials      547      547           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

let s = self.as_series().clone();
let mut s = self.as_series().clone();
if s.dtype().is_bool() {
s = s.cast(&DataType::UInt32)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should cast to IdxType.

let s = self.as_series().clone();
let original_type = self.as_series().dtype();
let mut s = self.as_series().clone();
if s.dtype().is_bool() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think we should cast here, but rather have a specialize algorithm for booleans.

ca,
options,
&rolling::no_nulls::rolling_min,
&rolling::nulls::rolling_min,
)
)?;
if original_type.is_bool() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think we should cast here, but rather have a specialize algorithm for booleans.

let mut s = self.as_series().clone();
if options.weights.is_some() {
s = s.to_float()?;
} else if s.dtype().is_bool() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think we should cast here, but rather have a specialize algorithm for booleans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants