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
Open
Show file tree
Hide file tree
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
64 changes: 53 additions & 11 deletions crates/polars-time/src/chunkedarray/rolling_window/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ pub trait SeriesOpsTime: AsSeries {
by: &Series,
options: RollingOptionsDynamicWindow,
) -> PolarsResult<Series> {
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.

}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
rolling_agg_by(
Expand All @@ -213,6 +217,8 @@ pub trait SeriesOpsTime: AsSeries {
let mut s = self.as_series().clone();
if options.weights.is_some() {
s = s.to_float()?;
} else if s.dtype().is_bool() {
s = s.cast(&DataType::UInt32)?;
}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
Expand Down Expand Up @@ -267,34 +273,52 @@ pub trait SeriesOpsTime: AsSeries {
by: &Series,
options: RollingOptionsDynamicWindow,
) -> PolarsResult<Series> {
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.

s = s.cast(&DataType::UInt32)?;
}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
rolling_agg_by(
let out = rolling_agg_by(
ca,
by,
options,
&super::rolling_kernels::no_nulls::rolling_min,
)
)?;
if original_type.is_bool() {
out.cast(original_type)
} else {
Ok(out)
}
})
}

/// Apply a rolling min to a Series.
#[cfg(feature = "rolling_window")]
fn rolling_min(&self, options: RollingOptionsFixedWindow) -> PolarsResult<Series> {
let original_type = self.as_series().dtype();
let mut s = self.as_series().clone();
if options.weights.is_some() {
s = s.to_float()?;
} else if s.dtype().is_bool() {
s = s.cast(&DataType::UInt32)?;
}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
rolling_agg(
let out = rolling_agg(
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.

out.cast(original_type)
} else {
Ok(out)
}
})
}

Expand All @@ -305,34 +329,52 @@ pub trait SeriesOpsTime: AsSeries {
by: &Series,
options: RollingOptionsDynamicWindow,
) -> PolarsResult<Series> {
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() {
s = s.cast(&DataType::UInt32)?;
}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
rolling_agg_by(
let out = rolling_agg_by(
ca,
by,
options,
&super::rolling_kernels::no_nulls::rolling_max,
)
)?;
if original_type.is_bool() {
out.cast(original_type)
} else {
Ok(out)
}
})
}

/// Apply a rolling max to a Series.
#[cfg(feature = "rolling_window")]
fn rolling_max(&self, options: RollingOptionsFixedWindow) -> PolarsResult<Series> {
let original_type = self.as_series().dtype();
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.

s = s.cast(&DataType::UInt32)?;
}

with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
rolling_agg(
let out = rolling_agg(
ca,
options,
&rolling::no_nulls::rolling_max,
&rolling::nulls::rolling_max,
)
)?;
if original_type.is_bool() {
out.cast(original_type)
} else {
Ok(out)
}
})
}

Expand Down
41 changes: 41 additions & 0 deletions py-polars/tests/unit/operations/rolling/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,19 @@ def test_rolling() -> None:
)


def test_rolling_min_max_sum_bool() -> None:
s = pl.Series("a", [True, True, False, False, True, None])
assert_series_equal(
s.rolling_min(2), pl.Series("a", [None, True, False, False, False, None])
)
assert_series_equal(
s.rolling_max(2), pl.Series("a", [None, True, True, False, True, None])
)
assert_series_equal(
s.rolling_sum(2), pl.Series("a", [None, 2, 1, 0, 1, None], dtype=pl.UInt32)
)


def test_rolling_by_date() -> None:
df = pl.DataFrame(
{
Expand All @@ -820,6 +833,34 @@ def test_rolling_by_date() -> None:
assert_frame_equal(result, expected)


def test_rolling_min_max_sum_bool_by_date_bool() -> None:
df = pl.DataFrame(
{
"dt": [
date(2020, 1, 1),
date(2020, 1, 2),
date(2020, 1, 3),
date(2020, 1, 4),
date(2020, 1, 5),
],
"val": [True, True, False, False, True],
}
).sort("dt")

assert_frame_equal(
df.with_columns(roll=pl.col("val").rolling_min_by("dt", "2d")),
df.with_columns(pl.Series("roll", [True, True, False, False, False])),
)
assert_frame_equal(
df.with_columns(roll=pl.col("val").rolling_max_by("dt", "2d")),
df.with_columns(pl.Series("roll", [True, True, True, False, True])),
)
assert_frame_equal(
df.with_columns(roll=pl.col("val").rolling_sum_by("dt", "2d")),
df.with_columns(pl.Series("roll", [1, 2, 1, 0, 1], dtype=pl.UInt32)),
)


@pytest.mark.parametrize("dtype", [pl.Int64, pl.Int32, pl.UInt64, pl.UInt32])
def test_rolling_by_integer(dtype: PolarsDataType) -> None:
df = pl.DataFrame({"val": [1, 2, 3]}).with_row_index()
Expand Down
Loading