Skip to content

Commit bf07b51

Browse files
committed
moved base.whitelist_method_generator to .generic
1 parent 0833dd6 commit bf07b51

File tree

3 files changed

+52
-55
lines changed

3 files changed

+52
-55
lines changed

pandas/_typing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from pandas.core.dtypes.dtypes import ExtensionDtype
1111
from pandas.core.dtypes.generic import (
12-
ABCExtensionArray, ABCIndexClass, ABCSeries, ABCSparseSeries)
12+
ABCDataFrame, ABCExtensionArray, ABCIndexClass, ABCSeries, ABCSparseSeries)
1313

1414
AnyArrayLike = TypeVar('AnyArrayLike',
1515
ABCExtensionArray,
@@ -22,3 +22,5 @@
2222
Timedelta)
2323
Dtype = Union[str, np.dtype, ExtensionDtype]
2424
FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
25+
26+
FrameOrSeries = TypeVar('FrameOrSeries', ABCSeries, ABCDataFrame)

pandas/core/groupby/base.py

-51
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
hold the whitelist of methods that are exposed on the
44
SeriesGroupBy and the DataFrameGroupBy objects.
55
"""
6-
from typing import TYPE_CHECKING, FrozenSet, Iterator, Type, Union
7-
86
from pandas.core.dtypes.common import is_list_like, is_scalar
97

10-
if TYPE_CHECKING:
11-
from pandas import DataFrame, Series
12-
from .groupby import GroupBy
13-
148

159
class GroupByMixin:
1610
"""
@@ -92,48 +86,3 @@ def _gotitem(self, key, ndim, subset=None):
9286

9387
cython_cast_blacklist = frozenset(['rank', 'count', 'size', 'idxmin',
9488
'idxmax'])
95-
96-
97-
def whitelist_method_generator(base: 'Type[GroupBy]',
98-
klass: 'Union[Type[DataFrame], Type[Series]]',
99-
whitelist: FrozenSet[str],
100-
) -> Iterator[str]:
101-
"""
102-
Yields all GroupBy member defs for DataFrame/Series names in whitelist.
103-
104-
Parameters
105-
----------
106-
base : Groupby class
107-
base class
108-
klass : DataFrame or Series class
109-
class where members are defined.
110-
whitelist : frozenset
111-
Set of names of klass methods to be constructed
112-
113-
Returns
114-
-------
115-
The generator yields a sequence of strings, each suitable for exec'ing,
116-
that define implementations of the named methods for DataFrameGroupBy
117-
or SeriesGroupBy.
118-
119-
Since we don't want to override methods explicitly defined in the
120-
base class, any such name is skipped.
121-
"""
122-
property_wrapper_template = \
123-
"""@property
124-
def %(name)s(self) :
125-
\"""%(doc)s\"""
126-
return self.__getattr__('%(name)s')"""
127-
128-
for name in whitelist:
129-
# don't override anything that was explicitly defined
130-
# in the base class
131-
if hasattr(base, name):
132-
continue
133-
# ugly, but we need the name string itself in the method.
134-
f = getattr(klass, name)
135-
doc = f.__doc__
136-
doc = doc if type(doc) == str else ''
137-
wrapper_template = property_wrapper_template
138-
params = {'name': name, 'doc': doc}
139-
yield wrapper_template % params

pandas/core/groupby/generic.py

+49-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from functools import partial
1212
from textwrap import dedent
1313
import typing
14-
from typing import Any, Callable, List, Union
14+
from typing import Any, Callable, FrozenSet, Iterator, List, Type, Union
1515
import warnings
1616

1717
import numpy as np
@@ -27,6 +27,7 @@
2727
is_integer_dtype, is_interval_dtype, is_numeric_dtype, is_scalar)
2828
from pandas.core.dtypes.missing import isna, notna
2929

30+
from pandas._typing import FrameOrSeries
3031
import pandas.core.algorithms as algorithms
3132
from pandas.core.base import DataError, SpecificationError
3233
import pandas.core.common as com
@@ -48,6 +49,51 @@
4849
AggScalar = Union[str, Callable[..., Any]]
4950

5051

52+
def whitelist_method_generator(base_class: Type[GroupBy],
53+
klass: Type[FrameOrSeries],
54+
whitelist: FrozenSet[str],
55+
) -> Iterator[str]:
56+
"""
57+
Yields all GroupBy member defs for DataFrame/Series names in whitelist.
58+
59+
Parameters
60+
----------
61+
base_class : Groupby class
62+
base class
63+
klass : DataFrame or Series class
64+
class where members are defined.
65+
whitelist : frozenset
66+
Set of names of klass methods to be constructed
67+
68+
Returns
69+
-------
70+
The generator yields a sequence of strings, each suitable for exec'ing,
71+
that define implementations of the named methods for DataFrameGroupBy
72+
or SeriesGroupBy.
73+
74+
Since we don't want to override methods explicitly defined in the
75+
base class, any such name is skipped.
76+
"""
77+
property_wrapper_template = \
78+
"""@property
79+
def %(name)s(self) :
80+
\"""%(doc)s\"""
81+
return self.__getattr__('%(name)s')"""
82+
83+
for name in whitelist:
84+
# don't override anything that was explicitly defined
85+
# in the base class
86+
if hasattr(base_class, name):
87+
continue
88+
# ugly, but we need the name string itself in the method.
89+
f = getattr(klass, name)
90+
doc = f.__doc__
91+
doc = doc if type(doc) == str else ''
92+
wrapper_template = property_wrapper_template
93+
params = {'name': name, 'doc': doc}
94+
yield wrapper_template % params
95+
96+
5197
class NDFrameGroupBy(GroupBy):
5298

5399
def _iterate_slices(self):
@@ -685,7 +731,7 @@ class SeriesGroupBy(GroupBy):
685731
# Make class defs of attributes on SeriesGroupBy whitelist
686732

687733
_apply_whitelist = base.series_apply_whitelist
688-
for _def_str in base.whitelist_method_generator(
734+
for _def_str in whitelist_method_generator(
689735
GroupBy, Series, _apply_whitelist):
690736
exec(_def_str)
691737

@@ -1289,7 +1335,7 @@ class DataFrameGroupBy(NDFrameGroupBy):
12891335

12901336
#
12911337
# Make class defs of attributes on DataFrameGroupBy whitelist.
1292-
for _def_str in base.whitelist_method_generator(
1338+
for _def_str in whitelist_method_generator(
12931339
GroupBy, DataFrame, _apply_whitelist):
12941340
exec(_def_str)
12951341

0 commit comments

Comments
 (0)