Skip to content

Commit bcca14f

Browse files
author
Kei
committed
Cache observed grouping/grouper instead of self obj
1 parent cbabce0 commit bcca14f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pandas/core/groupby/grouper.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -668,11 +668,15 @@ def groups(self) -> dict[Hashable, Index]:
668668
cats = Categorical.from_codes(codes, uniques, validate=False)
669669
return self._index.groupby(cats)
670670

671-
@cache_readonly
671+
@property
672672
def observed_grouping(self) -> Grouping:
673673
if self._observed:
674674
return self
675675

676+
return self._observed_grouping
677+
678+
@cache_readonly
679+
def _observed_grouping(self) -> Grouping:
676680
grouping = Grouping(
677681
self._index,
678682
self._orig_grouper,

pandas/core/groupby/ops.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -823,11 +823,15 @@ def result_index_and_ids(self) -> tuple[Index, npt.NDArray[np.intp]]:
823823

824824
return result_index, ids
825825

826-
@cache_readonly
826+
@property
827827
def observed_grouper(self) -> BaseGrouper:
828828
if all(ping._observed for ping in self.groupings):
829829
return self
830830

831+
return self._observed_grouper
832+
833+
@cache_readonly
834+
def _observed_grouper(self) -> BaseGrouper:
831835
groupings = [ping.observed_grouping for ping in self.groupings]
832836
grouper = BaseGrouper(self.axis, groupings, sort=self._sort, dropna=self.dropna)
833837
return grouper
@@ -1163,7 +1167,7 @@ def groupings(self) -> list[grouper.Grouping]:
11631167
)
11641168
return [ping]
11651169

1166-
@cache_readonly
1170+
@property
11671171
def observed_grouper(self) -> BinGrouper:
11681172
return self
11691173

0 commit comments

Comments
 (0)