Skip to content

Commit

Permalink
default negative offset in dynamic groupby (#2823)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 3, 2022
1 parent ea5ccc1 commit 82f1cb2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 4 additions & 4 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions py-polars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ venv:
@unset CONDA_PREFIX && source venv/bin/activate && maturin develop

clean:
@rm -r venv
@rm -r target/
@-rm -r venv
@cargo clean

pre-commit: venv
$(PYTHON_BIN)/isort .
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,7 @@ def groupby_dynamic(
period
length of the window, if None it is equal to 'every'
offset
offset of the window
offset of the window if None and period is None it will be equal to negative `every`
truncate
truncate the time value to the window lower bound
include_boundaries
Expand Down
9 changes: 6 additions & 3 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def groupby_dynamic(
period
length of the window, if None it is equal to 'every'
offset
offset of the window
offset of the window if None and period is None it will be equal to negative `every`
truncate
truncate the time value to the window lower bound
include_boundaries
Expand All @@ -848,10 +848,13 @@ def groupby_dynamic(
"""

if offset is None:
if period is None:
offset = f"-{every}"
else:
offset = "0ns"
if period is None:
period = every
if offset is None:
offset = "0ns"
by = _prepare_groupby_inputs(by)
lgb = self._ldf.groupby_dynamic(
index_column,
Expand Down
26 changes: 26 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,29 @@ def test_epoch() -> None:
assert dates.dt.epoch("d").series_equal(
(dates.dt.timestamp("ms") // (1000 * 3600 * 24)).cast(pl.Int32)
)


def test_default_negative_every_offset_dynamic_groupby() -> None:
# 2791
dts = [
datetime(2020, 1, 1),
datetime(2020, 1, 2),
datetime(2020, 2, 1),
datetime(2020, 3, 1),
]
df = pl.DataFrame({"dt": dts, "idx": range(len(dts))})
out = df.groupby_dynamic(index_column="dt", every="1mo", closed="right").agg(
pl.col("idx")
)

expected = pl.DataFrame(
{
"dt": [
datetime(2020, 1, 1, 0, 0),
datetime(2020, 1, 1, 0, 0),
datetime(2020, 3, 1, 0, 0),
],
"idx": [[0], [1, 2], [3]],
}
)
assert out.frame_equal(expected)

0 comments on commit 82f1cb2

Please sign in to comment.