Skip to content

REF: Grouper.grouper -> Grouper._gpr_index #41540

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 2 commits into from
May 21, 2021
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: 16 additions & 8 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ class Grouper:
axis: int
sort: bool
dropna: bool
_gpr_index: Index | None
_grouper: Index | None

_attributes: tuple[str, ...] = ("key", "level", "freq", "axis", "sort")

Expand Down Expand Up @@ -279,6 +281,7 @@ def __init__(
self.sort = sort

self.grouper = None
self._gpr_index = None
self.obj = None
self.indexer = None
self.binner = None
Expand All @@ -288,8 +291,11 @@ def __init__(

@final
@property
def ax(self):
return self.grouper
def ax(self) -> Index:
index = self._gpr_index
if index is None:
raise ValueError("_set_grouper must be called before ax is accessed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this hit in tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, in practice we always call _set_grouper before accessing this

return index

def _get_grouper(self, obj: FrameOrSeries, validate: bool = True):
"""
Expand Down Expand Up @@ -317,6 +323,7 @@ def _get_grouper(self, obj: FrameOrSeries, validate: bool = True):
validate=validate,
dropna=self.dropna,
)

return self.binner, self.grouper, self.obj

@final
Expand All @@ -338,14 +345,17 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):

# Keep self.grouper value before overriding
if self._grouper is None:
self._grouper = self.grouper
# TODO: What are we assuming about subsequent calls?
self._grouper = self._gpr_index
self._indexer = self.indexer

# the key must be a valid info item
if self.key is not None:
key = self.key
# The 'on' is already defined
if getattr(self.grouper, "name", None) == key and isinstance(obj, Series):
if getattr(self._gpr_index, "name", None) == key and isinstance(
obj, Series
):
# Sometimes self._grouper will have been resorted while
# obj has not. In this case there is a mismatch when we
# call self._grouper.take(obj.index) so we need to undo the sorting
Expand Down Expand Up @@ -390,10 +400,8 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False):
# error: Incompatible types in assignment (expression has type
# "FrameOrSeries", variable has type "None")
self.obj = obj # type: ignore[assignment]
# error: Incompatible types in assignment (expression has type "Index",
# variable has type "None")
self.grouper = ax # type: ignore[assignment]
return self.grouper
self._gpr_index = ax
return self._gpr_index

@final
@property
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def obj(self) -> FrameOrSeries: # type: ignore[override]

@property
def ax(self):
# we can infer that this is a PeriodIndex/DatetimeIndex/TimedeltaIndex,
# but skipping annotating bc the overrides overwhelming
return self.groupby.ax

@property
Expand Down