Skip to content

Commit 9531e09

Browse files
authored
REF: move only-used-once mixins to resample (#41058)
1 parent 8b1430d commit 9531e09

File tree

2 files changed

+57
-82
lines changed

2 files changed

+57
-82
lines changed

pandas/core/groupby/base.py

-75
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,8 @@
77

88
import collections
99

10-
from pandas._typing import final
11-
12-
from pandas.core.dtypes.common import (
13-
is_list_like,
14-
is_scalar,
15-
)
16-
17-
from pandas.core.base import PandasObject
18-
1910
OutputKey = collections.namedtuple("OutputKey", ["label", "position"])
2011

21-
22-
class ShallowMixin(PandasObject):
23-
_attributes: list[str] = []
24-
25-
@final
26-
def _shallow_copy(self, obj, **kwargs):
27-
"""
28-
return a new object with the replacement attributes
29-
"""
30-
if isinstance(obj, self._constructor):
31-
obj = obj.obj
32-
for attr in self._attributes:
33-
if attr not in kwargs:
34-
kwargs[attr] = getattr(self, attr)
35-
return self._constructor(obj, **kwargs)
36-
37-
38-
class GotItemMixin(PandasObject):
39-
"""
40-
Provide the groupby facilities to the mixed object.
41-
"""
42-
43-
_attributes: list[str]
44-
45-
@final
46-
def _gotitem(self, key, ndim, subset=None):
47-
"""
48-
Sub-classes to define. Return a sliced object.
49-
50-
Parameters
51-
----------
52-
key : string / list of selections
53-
ndim : {1, 2}
54-
requested ndim of result
55-
subset : object, default None
56-
subset to act on
57-
"""
58-
# create a new object to prevent aliasing
59-
if subset is None:
60-
# error: "GotItemMixin" has no attribute "obj"
61-
subset = self.obj # type: ignore[attr-defined]
62-
63-
# we need to make a shallow copy of ourselves
64-
# with the same groupby
65-
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
66-
67-
# Try to select from a DataFrame, falling back to a Series
68-
try:
69-
# error: "GotItemMixin" has no attribute "_groupby"
70-
groupby = self._groupby[key] # type: ignore[attr-defined]
71-
except IndexError:
72-
# error: "GotItemMixin" has no attribute "_groupby"
73-
groupby = self._groupby # type: ignore[attr-defined]
74-
75-
# error: Too many arguments for "GotItemMixin"
76-
# error: Unexpected keyword argument "groupby" for "GotItemMixin"
77-
# error: Unexpected keyword argument "parent" for "GotItemMixin"
78-
self = type(self)(
79-
subset, groupby=groupby, parent=self, **kwargs # type: ignore[call-arg]
80-
)
81-
self._reset_cache()
82-
if subset.ndim == 2 and (is_scalar(key) and key in subset or is_list_like(key)):
83-
self._selection = key
84-
return self
85-
86-
8712
# special case to prevent duplicate plots when catching exceptions when
8813
# forwarding methods from NDFrames
8914
plotting_methods = frozenset(["plot", "hist"])

pandas/core/resample.py

+57-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
T,
2424
TimedeltaConvertibleTypes,
2525
TimestampConvertibleTypes,
26+
final,
2627
)
2728
from pandas.compat.numpy import function as nv
2829
from pandas.errors import AbstractMethodError
@@ -39,16 +40,15 @@
3940

4041
import pandas.core.algorithms as algos
4142
from pandas.core.apply import ResamplerWindowApply
42-
from pandas.core.base import DataError
43+
from pandas.core.base import (
44+
DataError,
45+
PandasObject,
46+
)
4347
import pandas.core.common as com
4448
from pandas.core.generic import (
4549
NDFrame,
4650
_shared_docs,
4751
)
48-
from pandas.core.groupby.base import (
49-
GotItemMixin,
50-
ShallowMixin,
51-
)
5252
from pandas.core.groupby.generic import SeriesGroupBy
5353
from pandas.core.groupby.groupby import (
5454
BaseGroupBy,
@@ -86,7 +86,7 @@
8686
_shared_docs_kwargs: dict[str, str] = {}
8787

8888

89-
class Resampler(BaseGroupBy, ShallowMixin):
89+
class Resampler(BaseGroupBy, PandasObject):
9090
"""
9191
Class for resampling datetimelike data, a groupby-like operation.
9292
See aggregate, transform, and apply functions on this object.
@@ -141,6 +141,18 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs):
141141
if self.groupby is not None:
142142
self.groupby._set_grouper(self._convert_obj(obj), sort=True)
143143

144+
@final
145+
def _shallow_copy(self, obj, **kwargs):
146+
"""
147+
return a new object with the replacement attributes
148+
"""
149+
if isinstance(obj, self._constructor):
150+
obj = obj.obj
151+
for attr in self._attributes:
152+
if attr not in kwargs:
153+
kwargs[attr] = getattr(self, attr)
154+
return self._constructor(obj, **kwargs)
155+
144156
def __str__(self) -> str:
145157
"""
146158
Provide a nice str repr of our rolling object.
@@ -1018,11 +1030,13 @@ def h(self, _method=method):
10181030
setattr(Resampler, method, h)
10191031

10201032

1021-
class _GroupByMixin(GotItemMixin):
1033+
class _GroupByMixin(PandasObject):
10221034
"""
10231035
Provide the groupby facilities.
10241036
"""
10251037

1038+
_attributes: list[str]
1039+
10261040
def __init__(self, obj, *args, **kwargs):
10271041

10281042
parent = kwargs.pop("parent", None)
@@ -1064,6 +1078,42 @@ def func(x):
10641078
_downsample = _apply
10651079
_groupby_and_aggregate = _apply
10661080

1081+
@final
1082+
def _gotitem(self, key, ndim, subset=None):
1083+
"""
1084+
Sub-classes to define. Return a sliced object.
1085+
1086+
Parameters
1087+
----------
1088+
key : string / list of selections
1089+
ndim : {1, 2}
1090+
requested ndim of result
1091+
subset : object, default None
1092+
subset to act on
1093+
"""
1094+
# create a new object to prevent aliasing
1095+
if subset is None:
1096+
# error: "GotItemMixin" has no attribute "obj"
1097+
subset = self.obj # type: ignore[attr-defined]
1098+
1099+
# we need to make a shallow copy of ourselves
1100+
# with the same groupby
1101+
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
1102+
1103+
# Try to select from a DataFrame, falling back to a Series
1104+
try:
1105+
groupby = self._groupby[key]
1106+
except IndexError:
1107+
groupby = self._groupby
1108+
1109+
self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
1110+
self._reset_cache()
1111+
if subset.ndim == 2 and (
1112+
lib.is_scalar(key) and key in subset or lib.is_list_like(key)
1113+
):
1114+
self._selection = key
1115+
return self
1116+
10671117

10681118
class DatetimeIndexResampler(Resampler):
10691119
@property

0 commit comments

Comments
 (0)