Skip to content

Commit ceb0c8b

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: set _selection only in __init__ (pandas-dev#41403)
1 parent bbe0883 commit ceb0c8b

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

pandas/core/resample.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def __init__(
139139
groupby: TimeGrouper,
140140
axis: int = 0,
141141
kind=None,
142+
*,
143+
selection=None,
142144
**kwargs,
143145
):
144146
self.groupby = groupby
@@ -152,6 +154,7 @@ def __init__(
152154

153155
self.groupby._set_grouper(self._convert_obj(obj), sort=True)
154156
self.binner, self.grouper = self._get_binner()
157+
self._selection = selection
155158

156159
@final
157160
def _shallow_copy(self, obj, **kwargs):
@@ -1080,13 +1083,16 @@ def _gotitem(self, key, ndim, subset=None):
10801083
except IndexError:
10811084
groupby = self._groupby
10821085

1083-
self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
1084-
self._reset_cache()
1086+
selection = None
10851087
if subset.ndim == 2 and (
1086-
lib.is_scalar(key) and key in subset or lib.is_list_like(key)
1088+
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
10871089
):
1088-
self._selection = key
1089-
return self
1090+
selection = key
1091+
1092+
new_rs = type(self)(
1093+
subset, groupby=groupby, parent=self, selection=selection, **kwargs
1094+
)
1095+
return new_rs
10901096

10911097

10921098
class DatetimeIndexResampler(Resampler):

pandas/core/window/ewm.py

+3
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ def __init__(
268268
ignore_na: bool = False,
269269
axis: Axis = 0,
270270
times: str | np.ndarray | FrameOrSeries | None = None,
271+
*,
272+
selection=None,
271273
):
272274
super().__init__(
273275
obj=obj,
@@ -277,6 +279,7 @@ def __init__(
277279
closed=None,
278280
method="single",
279281
axis=axis,
282+
selection=selection,
280283
)
281284
self.com = com
282285
self.span = span

pandas/core/window/expanding.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ def __init__(
102102
center=None,
103103
axis: Axis = 0,
104104
method: str = "single",
105+
selection=None,
105106
):
106107
super().__init__(
107-
obj=obj, min_periods=min_periods, center=center, axis=axis, method=method
108+
obj=obj,
109+
min_periods=min_periods,
110+
center=center,
111+
axis=axis,
112+
method=method,
113+
selection=selection,
108114
)
109115

110116
def _get_window_indexer(self) -> BaseIndexer:

pandas/core/window/rolling.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def __init__(
123123
on: str | Index | None = None,
124124
closed: str | None = None,
125125
method: str = "single",
126+
*,
127+
selection=None,
126128
):
127129
self.obj = obj
128130
self.on = on
@@ -150,6 +152,8 @@ def __init__(
150152
f"invalid on specified as {self.on}, "
151153
"must be a column (of DataFrame), an Index or None"
152154
)
155+
156+
self._selection = selection
153157
self.validate()
154158

155159
@property
@@ -242,16 +246,22 @@ def _gotitem(self, key, ndim, subset=None):
242246
# create a new object to prevent aliasing
243247
if subset is None:
244248
subset = self.obj
245-
# TODO: Remove once win_type deprecation is enforced
249+
250+
# we need to make a shallow copy of ourselves
251+
# with the same groupby
246252
with warnings.catch_warnings():
253+
# TODO: Remove once win_type deprecation is enforced
247254
warnings.filterwarnings("ignore", "win_type", FutureWarning)
248-
self = type(self)(
249-
subset, **{attr: getattr(self, attr) for attr in self._attributes}
250-
)
251-
if subset.ndim == 2:
252-
if is_scalar(key) and key in subset or is_list_like(key):
253-
self._selection = key
254-
return self
255+
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
256+
257+
selection = None
258+
if subset.ndim == 2 and (
259+
(is_scalar(key) and key in subset) or is_list_like(key)
260+
):
261+
selection = key
262+
263+
new_win = type(self)(subset, selection=selection, **kwargs)
264+
return new_win
255265

256266
def __getattr__(self, attr: str):
257267
if attr in self._internal_names_set:

0 commit comments

Comments
 (0)