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

[REVIEW] Add GroupBy.dtypes #12783

Merged
merged 8 commits into from
Feb 16, 2023
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
18 changes: 18 additions & 0 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ def __iter__(self):
for i, name in enumerate(group_names):
yield name, grouped_values[offsets[i] : offsets[i + 1]]

@property
def dtypes(self):
non_grouped = [
name
for name in self.obj._data.names
if name not in self.grouping.names
]
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
all_dtypes = self.obj._dtypes
index = self.grouping.keys.unique().to_pandas()
df = pd.DataFrame(
{
name: pd.Series([all_dtypes[name]]).repeat(len(index))
for name in non_grouped
}
)
df.index = index
return df
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks much more simple than the pandas implementation, but I guess it makes sense.

galipremsagar marked this conversation as resolved.
Show resolved Hide resolved

@cached_property
def groups(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions python/cudf/cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2960,3 +2960,15 @@ def test_groupby_ngroup(by, ascending, df_ngroup):
expected = df_ngroup.to_pandas().groupby(by).ngroup(ascending=ascending)
actual = df_ngroup.groupby(by).ngroup(ascending=ascending)
assert_eq(expected, actual, check_dtype=False)


@pytest.mark.parametrize(
"groups", ["a", "b", "c", ["a", "c"], ["a", "b", "c"]]
)
def test_groupby_dtypes(groups):
df = cudf.DataFrame(
{"a": [1, 2, 3, 3], "b": ["x", "y", "z", "a"], "c": [10, 11, 12, 12]}
)
pdf = df.to_pandas()

assert_eq(pdf.groupby(groups).dtypes, df.groupby(groups).dtypes)