Skip to content

feat: add nunique method to Series/DataFrameGroupby #256

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

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions bigframes/core/groupby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ def any(self) -> df.DataFrame:
def count(self) -> df.DataFrame:
return self._aggregate_all(agg_ops.count_op)

def nunique(self) -> df.DataFrame:
return self._aggregate_all(agg_ops.nunique_op)

def cumsum(self, *args, numeric_only: bool = False, **kwargs) -> df.DataFrame:
if not numeric_only:
self._raise_on_non_numeric("cumsum")
Expand Down Expand Up @@ -442,6 +445,9 @@ def max(self, *args) -> series.Series:
def count(self) -> series.Series:
return self._aggregate(agg_ops.count_op)

def nunique(self) -> series.Series:
return self._aggregate(agg_ops.nunique_op)

def sum(self, *args) -> series.Series:
return self._aggregate(agg_ops.sum_op)

Expand Down
2 changes: 2 additions & 0 deletions tests/system/small/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ def test_dataframe_groupby_median(scalars_df_index, scalars_pandas_df_index):
("operator"),
[
(lambda x: x.count()),
(lambda x: x.nunique()),
(lambda x: x.any()),
(lambda x: x.all()),
],
ids=[
"count",
"nunique",
"any",
"all",
],
Expand Down
18 changes: 18 additions & 0 deletions third_party/bigframes_vendored/pandas/core/groupby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ def agg(self, func):
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def nunique(self):
"""
Return number of unique elements in the group.

Returns:
Series: Number of unique values within each group.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)


class DataFrameGroupBy(GroupBy):
def agg(self, func, **kwargs):
Expand Down Expand Up @@ -391,3 +400,12 @@ def agg(self, func, **kwargs):
DataFrame
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)

def nunique(self):
"""
Return DataFrame with counts of unique elements in each position.

Returns:
DataFrame
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)