Skip to content

Commit

Permalink
REF: Move compute to BinGrouper.result_index_and_ids (pandas-dev#57599)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored and pmhatre1 committed May 7, 2024
1 parent 31ef5ab commit 0412e31
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,31 +1110,29 @@ def indices(self):

@cache_readonly
def group_info(self) -> tuple[npt.NDArray[np.intp], int]:
ngroups = self.ngroups
rep = np.diff(np.r_[0, self.bins])

rep = ensure_platform_int(rep)
if ngroups == len(self.bins):
comp_ids = np.repeat(np.arange(ngroups), rep)
else:
comp_ids = np.repeat(np.r_[-1, np.arange(ngroups)], rep)

return (ensure_platform_int(comp_ids), ngroups)

@cache_readonly
def result_index(self) -> Index:
if len(self.binlabels) != 0 and isna(self.binlabels[0]):
return self.binlabels[1:]

return self.binlabels
return self.ids, self.ngroups

@cache_readonly
def codes(self) -> list[npt.NDArray[np.intp]]:
return [self.ids]

@cache_readonly
def result_index_and_ids(self):
return self.result_index, self.group_info[0]
result_index = self.binlabels
if len(self.binlabels) != 0 and isna(self.binlabels[0]):
result_index = result_index[1:]

ngroups = len(result_index)
rep = np.diff(np.r_[0, self.bins])

rep = ensure_platform_int(rep)
if ngroups == len(self.bins):
ids = np.repeat(np.arange(ngroups), rep)
else:
ids = np.repeat(np.r_[-1, np.arange(ngroups)], rep)
ids = ensure_platform_int(ids)

return result_index, ids

@property
def levels(self) -> list[Index]:
Expand Down

0 comments on commit 0412e31

Please sign in to comment.