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

Avoid shallow copies in groupby methods #17646

Open
wants to merge 2 commits into
base: branch-25.02
Choose a base branch
from
Open
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
30 changes: 16 additions & 14 deletions python/cudf/cudf/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from cudf.utils.utils import GetAttrGetItemMixin

if TYPE_CHECKING:
from collections.abc import Generator, Iterable
from collections.abc import Generator, Hashable, Iterable

from cudf._typing import (
AggType,
Expand Down Expand Up @@ -2445,7 +2445,7 @@ def _cov_or_corr(self, func, method_name):
# create expanded dataframe consisting all combinations of the
# struct columns-pairs to be used in the correlation or covariance
# i.e. (('col1', 'col1'), ('col1', 'col2'), ('col2', 'col2'))
column_names = self.grouping.values._column_names
column_names = self.grouping._values_column_names
num_cols = len(column_names)

column_pair_structs = {}
Expand Down Expand Up @@ -2679,10 +2679,8 @@ def diff(self, periods=1, axis=0):

if not axis == 0:
raise NotImplementedError("Only axis=0 is supported.")

values = self.obj.__class__._from_data(
self.grouping.values._data, self.obj.index
)
values = self.grouping.values
values.index = self.obj.index
return values - self.shift(periods=periods)

def _scan_fill(self, method: str, limit: int) -> DataFrameOrSeries:
Expand Down Expand Up @@ -2786,9 +2784,8 @@ def fillna(
raise ValueError("Method can only be of 'ffill', 'bfill'.")
return getattr(self, method, limit)()

values = self.obj.__class__._from_data(
self.grouping.values._data, self.obj.index
)
values = self.grouping.values
values.index = self.obj.index
return values.fillna(
value=value, inplace=inplace, axis=axis, limit=limit
)
Expand Down Expand Up @@ -3542,6 +3539,13 @@ def keys(self):
self._key_columns[0], name=self.names[0]
)

@property
def _values_column_names(self) -> list[Hashable]:
# If the key columns are in `obj`, filter them out
return [
x for x in self._obj._column_names if x not in self._named_columns
]

@property
def values(self) -> cudf.core.frame.Frame:
"""Return value columns as a frame.
Expand All @@ -3552,11 +3556,9 @@ def values(self) -> cudf.core.frame.Frame:

This is mainly used in transform-like operations.
"""
# If the key columns are in `obj`, filter them out
value_column_names = [
x for x in self._obj._column_names if x not in self._named_columns
]
value_columns = self._obj._data.select_by_label(value_column_names)
value_columns = self._obj._data.select_by_label(
self._values_column_names
)
return self._obj.__class__._from_data(value_columns)

def _handle_callable(self, by):
Expand Down
Loading