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: gather_every should work on agg context #13810

Merged
merged 1 commit into from
Jan 19, 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
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(
reswqa marked this conversation as resolved.
Show resolved Hide resolved
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)