Skip to content

Commit

Permalink
fix: group-by len (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Jun 22, 2024
1 parent 714e877 commit a1abbb6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions narwhals/_pandas_like/group_by.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ def agg_pandas( # noqa: PLR0913
assert expr._depth == 1
assert expr._root_names is not None
assert expr._output_names is not None
function_name = remove_prefix(expr._function_name, "col->")
function_name = POLARS_TO_PANDAS_AGGREGATIONS.get(
function_name, function_name
)
for root_name, output_name in zip(expr._root_names, expr._output_names):
name = remove_prefix(expr._function_name, "col->")
simple_aggregations[output_name] = (root_name, name)
simple_aggregations[output_name] = (root_name, function_name)

aggs = collections.defaultdict(list)
name_mapping = {}
Expand Down
9 changes: 9 additions & 0 deletions tests/group_by_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ def test_group_by_iter(constructor: Any) -> None:
for key, _df in df.group_by(["a", "b"]):
keys.append(key)
assert sorted(keys) == sorted(expected_keys)


@pytest.mark.parametrize("constructor", [pd.DataFrame, pl.DataFrame])
def test_group_by_len(constructor: Any) -> None:
result = (
nw.from_native(constructor(data)).group_by("a").agg(nw.col("b").len()).sort("a")
)
expected = {"a": [1, 3], "b": [2, 1]}
compare_dicts(result, expected)

0 comments on commit a1abbb6

Please sign in to comment.