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

Implement top/bottom-k aggregations in sort-based groupby #15272

Draft
wants to merge 7 commits into
base: branch-24.06
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Plumb through top-k to Python
  • Loading branch information
wence- committed Mar 20, 2024
commit c9357794ff8945e4b8208a7fd17747bbc05eb2ef
11 changes: 11 additions & 0 deletions python/cudf/cudf/_lib/aggregation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ class Aggregation:
def nth(cls, size):
return cls(pylibcudf.aggregation.nth_element(size))

@classmethod
def top_k(cls, size, descending=True):
return cls(
pylibcudf.aggregation.top_k(
size,
pylibcudf.types.Order.DESCENDING
if descending
else pylibcudf.types.Order.ASCENDING
)
)

@classmethod
def product(cls):
return cls(pylibcudf.aggregation.product())
Expand Down
5 changes: 5 additions & 0 deletions python/cudf/cudf/_lib/cpp/aggregation.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ cdef extern from "cudf/aggregation.hpp" namespace "cudf" nogil:
RANK
COLLECT_LIST
COLLECT_SET
TOP_K
PTX
CUDA
CORRELATION
Expand Down Expand Up @@ -135,6 +136,10 @@ cdef extern from "cudf/aggregation.hpp" namespace "cudf" nogil:
null_policy null_handling, null_equality nulls_equal, nan_equality nans_equal
) except +

cdef unique_ptr[T] make_top_k_aggregation[T](
size_type k, order order
) except +

cdef unique_ptr[T] make_udf_aggregation[T](
udf_type type,
string user_defined_aggregator,
Expand Down
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/aggregation.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ cpdef Aggregation collect_list(null_policy null_handling = *)

cpdef Aggregation collect_set(null_handling = *, nulls_equal = *, nans_equal = *)

cpdef Aggregation top_k(size_type k, order column_order = *)

cpdef Aggregation udf(str operation, DataType output_type)

cpdef Aggregation correlation(correlation_type type, size_type min_periods)
Expand Down
29 changes: 29 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/aggregation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ from cudf._lib.cpp.aggregation cimport (
make_std_aggregation,
make_sum_aggregation,
make_sum_of_squares_aggregation,
make_top_k_aggregation,
make_udf_aggregation,
make_variance_aggregation,
rank_method,
Expand Down Expand Up @@ -457,6 +458,34 @@ cpdef Aggregation collect_set(
)
)


cpdef Aggregation top_k(
size_type k,
order column_order = order.DESCENDING
):
"""Create a top_k aggregation.

Parameters
----------
k : size_type
Number of values to return.
column_order : order, default DESCENDING
Sort order within the groups.

Returns
-------
Aggregation
The top_k aggregation.
"""
return Aggregation.from_libcudf(
move(
make_top_k_aggregation[aggregation](
k, column_order
)
)
)


cpdef Aggregation udf(str operation, DataType output_type):
"""Create a udf aggregation.

Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,20 @@ def nth(self, n):
del self.obj._data["__groupbynth_order__"]
return result

@_cudf_nvtx_annotate
def topk(self, k, bottom=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that pandas calls this nlargest / nsmallest, but only exposes it for SeriesGroupBy and not DataFrameGroupBy. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.SeriesGroupBy.nlargest.html#pandas.core.groupby.SeriesGroupBy.nlargest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Their implementation is a bit more involved, since it has tie-breaking rules that necessitate (if keep != "first") introspecting the data rather than just the group size. So I can fill out nlargest/smallest for keep="first" and raise otherwise.

"""
Return the top-k values from each group

Parameters
----------
k : int
Number of values to obtain
bottom : bool
Should one return the bottom-k values?
"""
return self.agg(lambda x: x.top_k(k, descending=not bottom))

@_cudf_nvtx_annotate
def ngroup(self, ascending=True):
"""
Expand Down