Skip to content

REF: set _selection only in __init__ #41403

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 3 commits into from
May 17, 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
16 changes: 11 additions & 5 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def __init__(
groupby: TimeGrouper,
axis: int = 0,
kind=None,
*,
selection=None,
**kwargs,
):
self.groupby = groupby
Expand All @@ -152,6 +154,7 @@ def __init__(

self.groupby._set_grouper(self._convert_obj(obj), sort=True)
self.binner, self.grouper = self._get_binner()
self._selection = selection

@final
def _shallow_copy(self, obj, **kwargs):
Expand Down Expand Up @@ -1080,13 +1083,16 @@ def _gotitem(self, key, ndim, subset=None):
except IndexError:
groupby = self._groupby

self = type(self)(subset, groupby=groupby, parent=self, **kwargs)
self._reset_cache()
selection = None
if subset.ndim == 2 and (
lib.is_scalar(key) and key in subset or lib.is_list_like(key)
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
):
self._selection = key
return self
selection = key

new_rs = type(self)(
subset, groupby=groupby, parent=self, selection=selection, **kwargs
)
return new_rs


class DatetimeIndexResampler(Resampler):
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ def __init__(
ignore_na: bool = False,
axis: Axis = 0,
times: str | np.ndarray | FrameOrSeries | None = None,
*,
selection=None,
):
super().__init__(
obj=obj,
Expand All @@ -277,6 +279,7 @@ def __init__(
closed=None,
method="single",
axis=axis,
selection=selection,
)
self.com = com
self.span = span
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/window/expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@ def __init__(
center=None,
axis: Axis = 0,
method: str = "single",
selection=None,
):
super().__init__(
obj=obj, min_periods=min_periods, center=center, axis=axis, method=method
obj=obj,
min_periods=min_periods,
center=center,
axis=axis,
method=method,
selection=selection,
)

def _get_window_indexer(self) -> BaseIndexer:
Expand Down
26 changes: 18 additions & 8 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def __init__(
on: str | Index | None = None,
closed: str | None = None,
method: str = "single",
*,
selection=None,
):
self.obj = obj
self.on = on
Expand Down Expand Up @@ -150,6 +152,8 @@ def __init__(
f"invalid on specified as {self.on}, "
"must be a column (of DataFrame), an Index or None"
)

self._selection = selection
self.validate()

@property
Expand Down Expand Up @@ -242,16 +246,22 @@ def _gotitem(self, key, ndim, subset=None):
# create a new object to prevent aliasing
if subset is None:
subset = self.obj
# TODO: Remove once win_type deprecation is enforced

# we need to make a shallow copy of ourselves
# with the same groupby
with warnings.catch_warnings():
# TODO: Remove once win_type deprecation is enforced
warnings.filterwarnings("ignore", "win_type", FutureWarning)
self = type(self)(
subset, **{attr: getattr(self, attr) for attr in self._attributes}
)
if subset.ndim == 2:
if is_scalar(key) and key in subset or is_list_like(key):
self._selection = key
return self
kwargs = {attr: getattr(self, attr) for attr in self._attributes}

selection = None
if subset.ndim == 2 and (
(is_scalar(key) and key in subset) or is_list_like(key)
):
selection = key

new_win = type(self)(subset, selection=selection, **kwargs)
return new_win

def __getattr__(self, attr: str):
if attr in self._internal_names_set:
Expand Down