|
23 | 23 | T,
|
24 | 24 | TimedeltaConvertibleTypes,
|
25 | 25 | TimestampConvertibleTypes,
|
| 26 | + final, |
26 | 27 | )
|
27 | 28 | from pandas.compat.numpy import function as nv
|
28 | 29 | from pandas.errors import AbstractMethodError
|
|
39 | 40 |
|
40 | 41 | import pandas.core.algorithms as algos
|
41 | 42 | from pandas.core.apply import ResamplerWindowApply
|
42 |
| -from pandas.core.base import DataError |
| 43 | +from pandas.core.base import ( |
| 44 | + DataError, |
| 45 | + PandasObject, |
| 46 | +) |
43 | 47 | import pandas.core.common as com
|
44 | 48 | from pandas.core.generic import (
|
45 | 49 | NDFrame,
|
46 | 50 | _shared_docs,
|
47 | 51 | )
|
48 |
| -from pandas.core.groupby.base import ( |
49 |
| - GotItemMixin, |
50 |
| - ShallowMixin, |
51 |
| -) |
52 | 52 | from pandas.core.groupby.generic import SeriesGroupBy
|
53 | 53 | from pandas.core.groupby.groupby import (
|
54 | 54 | BaseGroupBy,
|
|
86 | 86 | _shared_docs_kwargs: dict[str, str] = {}
|
87 | 87 |
|
88 | 88 |
|
89 |
| -class Resampler(BaseGroupBy, ShallowMixin): |
| 89 | +class Resampler(BaseGroupBy, PandasObject): |
90 | 90 | """
|
91 | 91 | Class for resampling datetimelike data, a groupby-like operation.
|
92 | 92 | See aggregate, transform, and apply functions on this object.
|
@@ -141,6 +141,18 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs):
|
141 | 141 | if self.groupby is not None:
|
142 | 142 | self.groupby._set_grouper(self._convert_obj(obj), sort=True)
|
143 | 143 |
|
| 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 | + |
144 | 156 | def __str__(self) -> str:
|
145 | 157 | """
|
146 | 158 | Provide a nice str repr of our rolling object.
|
@@ -1018,11 +1030,13 @@ def h(self, _method=method):
|
1018 | 1030 | setattr(Resampler, method, h)
|
1019 | 1031 |
|
1020 | 1032 |
|
1021 |
| -class _GroupByMixin(GotItemMixin): |
| 1033 | +class _GroupByMixin(PandasObject): |
1022 | 1034 | """
|
1023 | 1035 | Provide the groupby facilities.
|
1024 | 1036 | """
|
1025 | 1037 |
|
| 1038 | + _attributes: list[str] |
| 1039 | + |
1026 | 1040 | def __init__(self, obj, *args, **kwargs):
|
1027 | 1041 |
|
1028 | 1042 | parent = kwargs.pop("parent", None)
|
@@ -1064,6 +1078,42 @@ def func(x):
|
1064 | 1078 | _downsample = _apply
|
1065 | 1079 | _groupby_and_aggregate = _apply
|
1066 | 1080 |
|
| 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 | + |
1067 | 1117 |
|
1068 | 1118 | class DatetimeIndexResampler(Resampler):
|
1069 | 1119 | @property
|
|
0 commit comments