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

Fix groupby head/tail for empty dataframe #13398

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def _head_tail(self, n, *, take_head: bool, preserve_order: bool):
# subsample the gather map from the full input ordering,
# rather than permuting the gather map of the output.
_, (ordering,), _ = self._groupby.groups(
[arange(0, self.obj._data.nrows)]
[arange(0, len(self.obj))]
Copy link
Contributor

Choose a reason for hiding this comment

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

The same error occurs (previously and after the fix for scans in #13389, which was me again sorry!). Although it's impossible to reach that code path right now because mimic pandas order for scans only happens if the dataframe is not empty.

But can you apply this patch too please?

diff --git a/python/cudf/cudf/core/groupby/groupby.py b/python/cudf/cudf/core/groupby/groupby.py
index fb242a49ad..b3be6d9de0 100644
--- a/python/cudf/cudf/core/groupby/groupby.py
+++ b/python/cudf/cudf/core/groupby/groupby.py
@@ -2277,9 +2277,7 @@ class GroupBy(Serializable, Reducible, Scannable):
         # result coming back from libcudf has null_count few rows than
         # the input, so we must produce an ordering from the full
         # input range.
-        _, (ordering,), _ = self._groupby.groups(
-            [arange(0, self.obj._data.nrows)]
-        )
+        _, (ordering,), _ = self._groupby.groups([arange(0, len(self.obj))])
         if self._dropna and any(
             c.has_nulls(include_nan=True) > 0
             for c in self.grouping._key_columns
@@ -2287,7 +2285,7 @@ class GroupBy(Serializable, Reducible, Scannable):
             # Scan aggregations with null/nan keys put nulls in the
             # corresponding output rows in pandas, to do that here
             # expand the result by reindexing.
-            ri = cudf.RangeIndex(0, self.obj._data.nrows)
+            ri = cudf.RangeIndex(0, len(self.obj))
             result.index = cudf.Index(ordering)
             # This reorders and expands
             result = result.reindex(ri)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

)
# Invert permutation from original order to groups on the
# subset of entries we want.
Expand Down
16 changes: 16 additions & 0 deletions python/cudf/cudf/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3231,3 +3231,19 @@ def test_head_tail(self, df, n, take_head, expected, preserve_order):
else:
actual = df.groupby("a").tail(n=n, preserve_order=preserve_order)
assert_eq(actual, expected)


def test_head_tail_empty():
# GH #13397

values = [1, 2, 3]
pdf = pd.DataFrame({}, index=values)
df = cudf.DataFrame({}, index=values)

expected = pdf.groupby(pd.Series(values)).head()
got = df.groupby(cudf.Series(values)).head()
assert_eq(expected, got)

expected = pdf.groupby(pd.Series(values)).tail()
got = df.groupby(cudf.Series(values)).tail()
assert_eq(expected, got)