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

PERF-#6710: Don't materialize index in _groupby_shuffle internal function #6707

Merged
merged 2 commits into from
Nov 7, 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
24 changes: 18 additions & 6 deletions modin/core/dataframe/pandas/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ def row_lengths(self):
self._row_lengths_cache = []
return self._row_lengths_cache

def __len__(self) -> int:
"""
Return length of index axis.

Returns
-------
int
"""
if self.has_materialized_index:
_len = len(self.index)
else:
_len = sum(self.row_lengths)
return _len

@property
def column_widths(self):
"""
Expand Down Expand Up @@ -2421,10 +2435,8 @@ def _apply_func_to_range_partitioning(

# don't want to inherit over-partitioning so doing this 'min' check
ideal_num_new_partitions = min(len(self._partitions), NPartitions.get())
m = len(self.index) / ideal_num_new_partitions
sampling_probability = (1 / m) * np.log(
ideal_num_new_partitions * len(self.index)
)
m = len(self) / ideal_num_new_partitions
sampling_probability = (1 / m) * np.log(ideal_num_new_partitions * len(self))
# If this df is overpartitioned, we try to sample each partition with probability
# greater than 1, which leads to an error. In this case, we can do one of the following
# two things. If there is only enough rows for one partition, and we have only 1 column
Expand All @@ -2435,8 +2447,8 @@ def _apply_func_to_range_partitioning(
if sampling_probability >= 1:
from modin.config import MinPartitionSize

ideal_num_new_partitions = round(len(self.index) / MinPartitionSize.get())
if len(self.index) < MinPartitionSize.get() or ideal_num_new_partitions < 2:
ideal_num_new_partitions = round(len(self) / MinPartitionSize.get())
if len(self) < MinPartitionSize.get() or ideal_num_new_partitions < 2:
# If the data is too small, we shouldn't try reshuffling/repartitioning but rather
# simply combine all partitions and apply the sorting to the whole dataframe
return self.combine_and_apply(func=func)
Expand Down
2 changes: 1 addition & 1 deletion modin/core/dataframe/pandas/dataframe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(
ideal_num_new_partitions: int,
**kwargs: dict,
):
self.frame_len = len(modin_frame.index)
self.frame_len = len(modin_frame)
self.ideal_num_new_partitions = ideal_num_new_partitions
self.columns = columns if is_list_like(columns) else [columns]
self.ascending = ascending
Expand Down
4 changes: 2 additions & 2 deletions modin/core/storage_formats/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3637,7 +3637,7 @@ def _groupby_shuffle(
# Higher API level won't pass empty data here unless the frame has delayed
# computations. FIXME: We apparently lose some laziness here (due to index access)
# because of the inability to process empty groupby natively.
if len(self.columns) == 0 or len(self.index) == 0:
if len(self.columns) == 0 or len(self._modin_frame) == 0:
return super().groupby_agg(
by, agg_func, axis, groupby_kwargs, agg_args, agg_kwargs, how, drop
)
Expand Down Expand Up @@ -3832,7 +3832,7 @@ def groupby_agg(
# Higher API level won't pass empty data here unless the frame has delayed
# computations. So we apparently lose some laziness here (due to index access)
# because of the inability to process empty groupby natively.
if len(self.columns) == 0 or len(self.index) == 0:
if len(self.columns) == 0 or len(self._modin_frame) == 0:
return super().groupby_agg(
by, agg_func, axis, groupby_kwargs, agg_args, agg_kwargs, how, drop
)
Expand Down
Loading