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

fix: In group_by_dynamic, period and every were getting applied in reverse order for the window upper boundary #19706

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
6 changes: 3 additions & 3 deletions crates/polars-time/src/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,15 @@ impl Iterator for BoundsIter<'_> {
// Issue is that `next` needs to return `Option`.
TimeUnit::Nanoseconds => {
self.bi.start = self.window.every.add_ns(self.bi.start, self.tz).unwrap();
self.bi.stop = self.window.every.add_ns(self.bi.stop, self.tz).unwrap();
self.bi.stop = self.window.period.add_ns(self.bi.start, self.tz).unwrap();
},
TimeUnit::Microseconds => {
self.bi.start = self.window.every.add_us(self.bi.start, self.tz).unwrap();
self.bi.stop = self.window.every.add_us(self.bi.stop, self.tz).unwrap();
self.bi.stop = self.window.period.add_us(self.bi.start, self.tz).unwrap();
},
TimeUnit::Milliseconds => {
self.bi.start = self.window.every.add_ms(self.bi.start, self.tz).unwrap();
self.bi.stop = self.window.every.add_ms(self.bi.stop, self.tz).unwrap();
self.bi.stop = self.window.period.add_ms(self.bi.start, self.tz).unwrap();
},
}
Some(out)
Expand Down
33 changes: 33 additions & 0 deletions py-polars/tests/unit/operations/test_group_by_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,36 @@ def test_group_by_dynamic_exclude_index_from_expansion_17075() -> None:
"n": [0, 2, 4, 6],
"m": [0, 2, 4, 6],
}


def test_group_by_dynamic_overlapping_19704() -> None:
df = pl.DataFrame(
{
"a": [datetime(2020, 1, 1), datetime(2020, 2, 1), datetime(2020, 3, 1)],
"b": [1, 2, 3],
}
)
result = df.group_by_dynamic(
"a", every="1mo", period="45d", include_boundaries=True
).agg(pl.col("b").sum())
expected = pl.DataFrame(
{
"_lower_boundary": [
datetime(2020, 1, 1, 0, 0),
datetime(2020, 2, 1, 0, 0),
datetime(2020, 3, 1, 0, 0),
],
"_upper_boundary": [
datetime(2020, 2, 15, 0, 0),
datetime(2020, 3, 17, 0, 0),
datetime(2020, 4, 15, 0, 0),
],
"a": [
datetime(2020, 1, 1, 0, 0),
datetime(2020, 2, 1, 0, 0),
datetime(2020, 3, 1, 0, 0),
],
"b": [3, 5, 3],
}
)
assert_frame_equal(result, expected)
Loading