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] Implement .describe() for DataFrameGroupBy #8179

Merged
merged 14 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
42 changes: 42 additions & 0 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
from cudf.utils.utils import GetAttrGetItemMixin, cached_property


# The three functions below return the quantiles [25%, 50%, 75%]
# respectively, which are called in the describe() method to ouput
# the summary stats of a GroupBy object
def _quantile_25(x):
return x.quantile(0.25)


def _quantile_50(x):
return x.quantile(0.50)


def _quantile_75(x):
return x.quantile(0.75)


# Note that all valid aggregation methods (e.g. GroupBy.min) are bound to the
# class after its definition (see below).
class GroupBy(Serializable):
Expand Down Expand Up @@ -597,6 +612,33 @@ def func(x):

return self.agg(func)

def describe(self):
skirui-source marked this conversation as resolved.
Show resolved Hide resolved
"""Return descriptive stats of the values in each group"""

res = self.agg(
[
"count",
"mean",
"std",
"min",
_quantile_25,
_quantile_50,
_quantile_75,
"max",
]
)
res.columns = [
"count",
"mean",
"std",
"min",
"25%",
"50%",
"75%",
"max",
]
skirui-source marked this conversation as resolved.
Show resolved Hide resolved
return res

def sum(self):
"""Compute the column-wise sum of the values in each group."""
return self.agg("sum")
Expand Down
14 changes: 14 additions & 0 deletions python/cudf/cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,3 +1666,17 @@ def test_groupby_mix_agg_scan():
gb.agg(func[1:])
with pytest.raises(NotImplementedError, match=err_msg):
gb.agg(func)


@pytest.mark.parametrize(
"data", [{"Speed": [380.0, 370.0, 24.0, 26.0], "Score": [50, 30, 90, 80]}],
skirui-source marked this conversation as resolved.
Show resolved Hide resolved
)
@pytest.mark.parametrize("group", ["Score", "Speed"])
def test_groupby_describe(data, group):
pdf = pd.DataFrame(data)
gdf = cudf.from_pandas(pdf)

got = gdf.groupby(group).describe()
expect = pdf.groupby(group).describe()

assert_groupby_results_equal(expect, got, check_dtype=False)
kkraus14 marked this conversation as resolved.
Show resolved Hide resolved