Skip to content

Commit 49d96b4

Browse files
authored
REF: de-duplicate _gotitem helper (#53874)
* REF: de-duplicate _gotitem helper * mypy fixup
1 parent 3e6adac commit 49d96b4

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

pandas/core/base.py

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
)
7878

7979
from pandas import (
80+
DataFrame,
8081
Index,
8182
Series,
8283
)
@@ -254,6 +255,21 @@ def _gotitem(self, key, ndim: int, subset=None):
254255
"""
255256
raise AbstractMethodError(self)
256257

258+
@final
259+
def _infer_selection(self, key, subset: Series | DataFrame):
260+
"""
261+
Infer the `selection` to pass to our constructor in _gotitem.
262+
"""
263+
# Shared by Rolling and Resample
264+
selection = None
265+
if subset.ndim == 2 and (
266+
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
267+
):
268+
selection = key
269+
elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name:
270+
selection = key
271+
return selection
272+
257273
def aggregate(self, func, *args, **kwargs):
258274
raise AbstractMethodError(self)
259275

pandas/core/resample.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242

4343
import pandas.core.algorithms as algos
4444
from pandas.core.apply import ResamplerWindowApply
45-
from pandas.core.base import PandasObject
45+
from pandas.core.base import (
46+
PandasObject,
47+
SelectionMixin,
48+
)
4649
import pandas.core.common as com
4750
from pandas.core.generic import (
4851
NDFrame,
@@ -1293,7 +1296,7 @@ def quantile(self, q: float | AnyArrayLike = 0.5, **kwargs):
12931296
return self._downsample("quantile", q=q, **kwargs)
12941297

12951298

1296-
class _GroupByMixin(PandasObject):
1299+
class _GroupByMixin(PandasObject, SelectionMixin):
12971300
"""
12981301
Provide the groupby facilities.
12991302
"""
@@ -1385,13 +1388,7 @@ def _gotitem(self, key, ndim, subset=None):
13851388
except IndexError:
13861389
groupby = self._groupby
13871390

1388-
selection = None
1389-
if subset.ndim == 2 and (
1390-
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
1391-
):
1392-
selection = key
1393-
elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name:
1394-
selection = key
1391+
selection = self._infer_selection(key, subset)
13951392

13961393
new_rs = type(self)(
13971394
groupby=groupby,

pandas/core/window/rolling.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
ensure_float64,
3838
is_bool,
3939
is_integer,
40-
is_list_like,
4140
is_numeric_dtype,
42-
is_scalar,
4341
needs_i8_conversion,
4442
)
4543
from pandas.core.dtypes.generic import (
@@ -302,14 +300,7 @@ def _gotitem(self, key, ndim, subset=None):
302300
# with the same groupby
303301
kwargs = {attr: getattr(self, attr) for attr in self._attributes}
304302

305-
selection = None
306-
if subset.ndim == 2 and (
307-
(is_scalar(key) and key in subset) or is_list_like(key)
308-
):
309-
selection = key
310-
elif subset.ndim == 1 and is_scalar(key) and key == subset.name:
311-
selection = key
312-
303+
selection = self._infer_selection(key, subset)
313304
new_win = type(self)(subset, selection=selection, **kwargs)
314305
return new_win
315306

0 commit comments

Comments
 (0)