Skip to content

Commit

Permalink
fix: gather_every should work on agg context (pola-rs#13810)
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa authored and r-brink committed Jan 24, 2024
1 parent 8d1867f commit 0913be8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ impl PyExpr {
fn gather_every(&self, n: usize, offset: usize) -> Self {
self.inner
.clone()
.map(
.apply(
move |s: Series| {
polars_ensure!(n > 0, InvalidOperation: "gather_every(n): n can't be zero");
Ok(Some(s.gather_every(n, offset)))
Expand Down
17 changes: 17 additions & 0 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,23 @@ def test_gather_every() -> None:
assert_frame_equal(expected_df, df.gather_every(2, offset=1))


def test_gather_every_agg() -> None:
df = pl.DataFrame(
{
"g": [1, 1, 1, 2, 2, 2],
"a": ["a", "b", "c", "d", "e", "f"],
}
)
out = df.group_by(pl.col("g")).agg(pl.col("a").gather_every(2)).sort("g")
expected = pl.DataFrame(
{
"g": [1, 2],
"a": [["a", "c"], ["d", "f"]],
}
)
assert_frame_equal(out, expected)


def test_take_misc(fruits_cars: pl.DataFrame) -> None:
df = fruits_cars

Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/namespaces/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,10 @@ def test_list_get_logical_type() -> None:
dtype=pl.Date,
)
assert_series_equal(out, expected)


def test_list_eval_gater_every_13410() -> None:
df = pl.DataFrame({"a": [[1, 2, 3], [4, 5, 6]]})
out = df.with_columns(result=pl.col("a").list.eval(pl.element().gather_every(2)))
expected = pl.DataFrame({"a": [[1, 2, 3], [4, 5, 6]], "result": [[1, 3], [4, 6]]})
assert_frame_equal(out, expected)

0 comments on commit 0913be8

Please sign in to comment.