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: fix pyarrow len aggregation behaviour for depth-1 exprs #1589

Conversation

AlessandroMiola
Copy link
Contributor

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

Adds a specific condition for dealing with pyarrow depth-1 len expressions in group-by contexts.

Comment on lines +100 to +119
@pytest.mark.parametrize(
("expr", "expected"),
[
(nw.col("b").sum(), {"a": [1, 2], "b": [3, 3]}),
(nw.col("b").mean(), {"a": [1, 2], "b": [1.5, 3]}),
(nw.col("b").max(), {"a": [1, 2], "b": [2, 3]}),
(nw.col("b").min(), {"a": [1, 2], "b": [1, 3]}),
(nw.col("b").std(), {"a": [1, 2], "b": [0.707107, None]}),
(nw.col("b").len(), {"a": [1, 2], "b": [3, 1]}),
(nw.col("b").n_unique(), {"a": [1, 2], "b": [3, 1]}),
(nw.col("b").count(), {"a": [1, 2], "b": [2, 1]}),
],
)
def test_group_by_depth_1_agg(
constructor: Constructor,
expr: nw.Expr,
expected: dict[str, list[int | float]],
) -> None:
data = {"a": [1, 1, 1, 2], "b": [1, None, 2, 3]}
result = nw.from_native(constructor(data)).group_by("a").agg(expr).sort("a")
Copy link
Contributor Author

@AlessandroMiola AlessandroMiola Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also median is failing here for pyarrow πŸ€”, reason why I've left it out and maintained the original test for it. Somehow, though, we're pointing it out in docs that median's results might slightly differ across backends due to differences in the underlying algorithms...

@AlessandroMiola AlessandroMiola marked this pull request as ready for review December 14, 2024 09:09
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a tonne for spotting and doing this, just got a comment

Comment on lines 173 to 178
function_name = POLARS_TO_ARROW_AGGREGATIONS.get(function_name, function_name)

option = get_function_name_option(function_name)
if expr._function_name != "col->len":
option = get_function_name_option(function_name)
else:
option = pc.CountOptions(mode="all")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be cleaner to have

function_name, option = POLARS_TO_ARROW_AGGREGATIONS[function_name]

, and map to all the options in POLARS_TO_ARROW_AGGREGATIONS?

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @AlessandroMiola !

@MarcoGorelli MarcoGorelli merged commit 3eec7a7 into narwhals-dev:main Dec 14, 2024
23 checks passed
@AlessandroMiola AlessandroMiola deleted the fix-depth-1-len-in-pyarrow-group-by-agg branch December 14, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants