Skip to content

REF: de-duplicate _gotitem helper #53874

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
Jun 27, 2023
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: 16 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
)

from pandas import (
DataFrame,
Index,
Series,
)
Expand Down Expand Up @@ -254,6 +255,21 @@ def _gotitem(self, key, ndim: int, subset=None):
"""
raise AbstractMethodError(self)

@final
def _infer_selection(self, key, subset: Series | DataFrame):
"""
Infer the `selection` to pass to our constructor in _gotitem.
"""
# Shared by Rolling and Resample
selection = None
if subset.ndim == 2 and (
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
):
selection = key
elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name:
selection = key
return selection

def aggregate(self, func, *args, **kwargs):
raise AbstractMethodError(self)

Expand Down
15 changes: 6 additions & 9 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

import pandas.core.algorithms as algos
from pandas.core.apply import ResamplerWindowApply
from pandas.core.base import PandasObject
from pandas.core.base import (
PandasObject,
SelectionMixin,
)
import pandas.core.common as com
from pandas.core.generic import (
NDFrame,
Expand Down Expand Up @@ -1293,7 +1296,7 @@ def quantile(self, q: float | AnyArrayLike = 0.5, **kwargs):
return self._downsample("quantile", q=q, **kwargs)


class _GroupByMixin(PandasObject):
class _GroupByMixin(PandasObject, SelectionMixin):
"""
Provide the groupby facilities.
"""
Expand Down Expand Up @@ -1385,13 +1388,7 @@ def _gotitem(self, key, ndim, subset=None):
except IndexError:
groupby = self._groupby

selection = None
if subset.ndim == 2 and (
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
):
selection = key
elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name:
selection = key
selection = self._infer_selection(key, subset)

new_rs = type(self)(
groupby=groupby,
Expand Down
11 changes: 1 addition & 10 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
ensure_float64,
is_bool,
is_integer,
is_list_like,
is_numeric_dtype,
is_scalar,
needs_i8_conversion,
)
from pandas.core.dtypes.generic import (
Expand Down Expand Up @@ -302,14 +300,7 @@ def _gotitem(self, key, ndim, subset=None):
# with the same groupby
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
elif subset.ndim == 1 and is_scalar(key) and key == subset.name:
selection = key

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

Expand Down