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

[FEA] Add support for cudf.NamedAgg #16744

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 python/cudf/cudf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
ListDtype,
StructDtype,
)
from cudf.core.groupby import Grouper
from cudf.core.groupby import Grouper, NamedAgg
from cudf.core.index import (
BaseIndex,
CategoricalIndex,
Expand Down
5 changes: 3 additions & 2 deletions python/cudf/cudf/core/groupby/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

from cudf.core.groupby.groupby import GroupBy, Grouper
from cudf.core.groupby.groupby import GroupBy, Grouper, NamedAgg

__all__ = [
"GroupBy",
"Grouper",
"NamedAgg",
]
48 changes: 45 additions & 3 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ def _is_row_of(chunk, obj):
)


NamedAgg = pd.NamedAgg


NamedAgg.__doc__ = """
Helper for column specific aggregation with control over output column names.

Subclass of typing.NamedTuple.

Parameters
----------
column : Hashable
Column label in the DataFrame to apply aggfunc.
aggfunc : function or str
Function to apply to the provided column.

Examples
--------
>>> df = cudf.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], 1: [10, 11, 12]})
>>> agg_a = cudf.NamedAgg(column="a", aggfunc="min")
>>> agg_1 = cudf.NamedAgg(column=1, aggfunc=lambda x: x.mean())
>>> df.groupby("key").agg(result_a=agg_a, result_1=agg_1)
result_a result_1
key
1 -1 10.5
2 1 12.0
"""


groupby_doc_template = textwrap.dedent(
"""Group using a mapper or by a Series of columns.

Expand Down Expand Up @@ -1296,9 +1324,23 @@ def _normalize_aggs(
columns = values._columns
aggs_per_column = (aggs,) * len(columns)
elif not aggs and kwargs:
column_names, aggs_per_column = kwargs.keys(), kwargs.values()
columns = tuple(self.obj._data[x[0]] for x in kwargs.values())
aggs_per_column = tuple(x[1] for x in kwargs.values())
column_names = kwargs.keys()

def _raise_invalid_type(x):
raise TypeError(
f"Invalid keyword argument {x} of type {type(x)} was passed to agg"
Matt711 marked this conversation as resolved.
Show resolved Hide resolved
)

columns, aggs_per_column = zip(
*(
(self.obj._data[x[0]], x[1])
if isinstance(x, tuple)
else (self.obj._data[x.column], x.pyfunc)
Matt711 marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(x, NamedAgg)
Matt711 marked this conversation as resolved.
Show resolved Hide resolved
else (_raise_invalid_type(x))
Matt711 marked this conversation as resolved.
Show resolved Hide resolved
for x in kwargs.values()
)
)
else:
raise TypeError("Must provide at least one aggregation function.")

Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/groupby/test_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ def test_dataframe_agg(attr, func):
)

assert_eq(agg, pd_agg)

agg = getattr(df.groupby("a"), attr)(
foo=cudf.NamedAgg(column="b", aggfunc=func),
bar=cudf.NamedAgg(column="a", aggfunc=func),
)
pd_agg = getattr(pdf.groupby(["a"]), attr)(
foo=("b", func), bar=("a", func)
)

assert_eq(agg, pd_agg)
Loading