Skip to content

PERF: avoid doing check at each step in loop #40780

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

Merged
merged 1 commit into from
Apr 5, 2021
Merged
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: 18 additions & 12 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def get_iterator(
"""
splitter = self._get_splitter(data, axis=axis)
keys = self._get_group_keys()
for key, (i, group) in zip(keys, splitter):
for key, group in zip(keys, splitter):
yield key, group.__finalize__(data, method="groupby")

@final
Expand Down Expand Up @@ -411,21 +411,27 @@ def apply(self, f: F, data: FrameOrSeries, axis: int = 0):
if len(result_values) == len(group_keys):
return group_keys, result_values, mutated

for key, (i, group) in zip(group_keys, splitter):
object.__setattr__(group, "name", key)

if result_values is None:
# result_values is None if fast apply path wasn't taken
# or fast apply aborted with an unexpected exception.
# In either case, initialize the result list and perform
# the slow iteration.
if result_values is None:
result_values = []

result_values = []
skip_first = False
else:
# If result_values is not None we're in the case that the
# fast apply loop was broken prematurely but we have
# already the result for the first group which we can reuse.
elif i == 0:
continue
skip_first = True

# This calls DataSplitter.__iter__
zipped = zip(group_keys, splitter)
if skip_first:
# pop the first item from the front of the iterator
next(zipped)

for key, group in zipped:
object.__setattr__(group, "name", key)

# group might be modified
group_axes = group.axes
Expand Down Expand Up @@ -779,7 +785,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):

splitter = get_splitter(obj, group_index, ngroups, axis=0)

for label, group in splitter:
for label, group in enumerate(splitter):

# Each step of this loop corresponds to
# libreduction._BaseGrouper._apply_to_group
Expand Down Expand Up @@ -1012,8 +1018,8 @@ def __iter__(self):

starts, ends = lib.generate_slices(self.slabels, self.ngroups)

for i, (start, end) in enumerate(zip(starts, ends)):
yield i, self._chop(sdata, slice(start, end))
for start, end in zip(starts, ends):
yield self._chop(sdata, slice(start, end))

@cache_readonly
def sorted_data(self) -> FrameOrSeries:
Expand Down