Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
32 changes: 32 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,38 @@ def __iter__(self):
for i, name in enumerate(group_names):
yield name, grouped_values[offsets[i] : offsets[i + 1]]

@property
def dtypes(self):
"""
Return the dtypes in this group.

Returns
-------
pandas.DataFrame
The data type of each column of the group.

Examples
--------
>>> import cudf
>>> df = cudf.DataFrame({'a': [1, 2, 3, 3], 'b': ['x', 'y', 'z', 'a'],
... 'c':[10, 11, 12, 12]})
>>> df.groupby("a").dtypes
b c
a
1 object int64
2 object int64
3 object int64
"""
index = self.grouping.keys.unique().to_pandas()
result = pd.DataFrame(
{
name: pd.Series([self.obj._dtypes[name]]).repeat(len(index))
for name in self.grouping.values._column_names
},
)
result.index = index
return result

@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)