Skip to content

REF: Resampler.groupby -> _timegrouper #51135

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
Feb 3, 2023
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
41 changes: 22 additions & 19 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Resampler(BaseGroupBy, PandasObject):
"""

grouper: BinGrouper
_timegrouper: TimeGrouper
exclusions: frozenset[Hashable] = frozenset() # for SelectionMixin compat

# to the groupby descriptor
Expand All @@ -152,7 +153,7 @@ def __init__(
selection=None,
**kwargs,
) -> None:
self.groupby = groupby
self._timegrouper = groupby
self.keys = None
self.sort = True
# error: Incompatible types in assignment (expression has type "Union
Expand All @@ -162,11 +163,11 @@ def __init__(
self.group_keys = group_keys
self.as_index = True

self.groupby._set_grouper(self._convert_obj(obj), sort=True)
self._timegrouper._set_grouper(self._convert_obj(obj), sort=True)
self.binner, self.grouper = self._get_binner()
self._selection = selection
if self.groupby.key is not None:
self.exclusions = frozenset([self.groupby.key])
if self._timegrouper.key is not None:
self.exclusions = frozenset([self._timegrouper.key])
else:
self.exclusions = frozenset()

Expand All @@ -187,17 +188,17 @@ def __str__(self) -> str:
Provide a nice str repr of our rolling object.
"""
attrs = (
f"{k}={getattr(self.groupby, k)}"
f"{k}={getattr(self._timegrouper, k)}"
for k in self._attributes
if getattr(self.groupby, k, None) is not None
if getattr(self._timegrouper, k, None) is not None
)
return f"{type(self).__name__} [{', '.join(attrs)}]"

def __getattr__(self, attr: str):
if attr in self._internal_names_set:
return object.__getattribute__(self, attr)
if attr in self._attributes:
return getattr(self.groupby, attr)
return getattr(self._timegrouper, attr)
if attr in self.obj:
return self[attr]

Expand All @@ -208,13 +209,13 @@ def __getattr__(self, attr: str):
def obj(self) -> NDFrame: # type: ignore[override]
# error: Incompatible return value type (got "Optional[Any]",
# expected "NDFrameT")
return self.groupby.obj # type: ignore[return-value]
return self._timegrouper.obj # type: ignore[return-value]

@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
return self._timegrouper.ax

@property
def _from_selection(self) -> bool:
Expand All @@ -223,8 +224,8 @@ def _from_selection(self) -> bool:
"""
# upsampling and PeriodIndex resampling do not work
# with selection, this state used to catch and raise an error
return self.groupby is not None and (
self.groupby.key is not None or self.groupby.level is not None
return self._timegrouper is not None and (
self._timegrouper.key is not None or self._timegrouper.level is not None
)

def _convert_obj(self, obj: NDFrameT) -> NDFrameT:
Expand Down Expand Up @@ -252,7 +253,7 @@ def _get_binner(self):
"""
binner, bins, binlabels = self._get_binner_for_time()
assert len(bins) == len(binlabels)
bin_grouper = BinGrouper(bins, binlabels, indexer=self.groupby.indexer)
bin_grouper = BinGrouper(bins, binlabels, indexer=self._timegrouper.indexer)
return binner, bin_grouper

@Substitution(
Expand Down Expand Up @@ -391,7 +392,9 @@ def transform(self, arg, *args, **kwargs):
2018-01-01 01:00:00 NaN
Freq: H, dtype: float64
"""
return self._selected_obj.groupby(self.groupby).transform(arg, *args, **kwargs)
return self._selected_obj.groupby(self._timegrouper).transform(
arg, *args, **kwargs
)

def _downsample(self, f, **kwargs):
raise AbstractMethodError(self)
Expand Down Expand Up @@ -1168,7 +1171,7 @@ def __init__(self, obj, parent=None, groupby=None, key=None, **kwargs) -> None:
self.key = key

self._groupby = groupby
self.groupby = copy.copy(parent.groupby)
self._timegrouper = copy.copy(parent._timegrouper)

@no_type_check
def _apply(self, f, *args, **kwargs):
Expand All @@ -1178,7 +1181,7 @@ def _apply(self, f, *args, **kwargs):
"""

def func(x):
x = self._shallow_copy(x, groupby=self.groupby)
x = self._shallow_copy(x, groupby=self._timegrouper)

if isinstance(f, str):
return getattr(x, f)(**kwargs)
Expand Down Expand Up @@ -1243,8 +1246,8 @@ def _get_binner_for_time(self):

# this is how we are actually creating the bins
if self.kind == "period":
return self.groupby._get_time_period_bins(self.ax)
return self.groupby._get_time_bins(self.ax)
return self._timegrouper._get_time_period_bins(self.ax)
return self._timegrouper._get_time_bins(self.ax)

def _downsample(self, how, **kwargs):
"""
Expand Down Expand Up @@ -1373,7 +1376,7 @@ def _resampler_for_grouping(self):
def _get_binner_for_time(self):
if self.kind == "timestamp":
return super()._get_binner_for_time()
return self.groupby._get_period_bins(self.ax)
return self._timegrouper._get_period_bins(self.ax)

def _convert_obj(self, obj: NDFrameT) -> NDFrameT:
obj = super()._convert_obj(obj)
Expand Down Expand Up @@ -1483,7 +1486,7 @@ def _resampler_for_grouping(self):
return TimedeltaIndexResamplerGroupby

def _get_binner_for_time(self):
return self.groupby._get_time_delta_bins(self.ax)
return self._timegrouper._get_time_delta_bins(self.ax)

def _adjust_binner_for_upsample(self, binner):
"""
Expand Down