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

Correct dtype of count aggregations on empty dataframes #14473

Merged
merged 2 commits into from
Nov 22, 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: 3 additions & 3 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,9 @@ def agg(self, func):
# Structs lose their labels which we reconstruct here
col = col._with_type_metadata(cudf.ListDtype(orig_dtype))

if (
if agg_kind in {"COUNT", "SIZE"}:
data[key] = col.astype("int64")
elif (
self.obj.empty
and (
isinstance(agg_name, str)
Expand All @@ -609,8 +611,6 @@ def agg(self, func):
)
):
data[key] = col.astype(orig_dtype)
elif agg_kind in {"COUNT", "SIZE"}:
data[key] = col.astype("int64")
else:
data[key] = col
data = ColumnAccessor(data, multiindex=multilevel)
Expand Down
18 changes: 18 additions & 0 deletions python/cudf/cudf/tests/groupby/test_agg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2023, NVIDIA CORPORATION.
import numpy as np
import pytest

import cudf


@pytest.mark.parametrize(
"empty",
[True, False],
ids=["empty", "nonempty"],
)
def test_agg_count_dtype(empty):
df = cudf.DataFrame({"a": [1, 2, 1], "c": ["a", "b", "c"]})
if empty:
df = df.iloc[:0]
result = df.groupby("a").agg({"c": "count"})
assert result["c"].dtype == np.dtype("int64")