Skip to content

Commit e9e034b

Browse files
authored
CLN/TST: Clean groupby.test_allowlist (#52537)
* CLN/TST: Clean groupby.test_allowlist * Add description to test_api.
1 parent 328a620 commit e9e034b

File tree

2 files changed

+129
-177
lines changed

2 files changed

+129
-177
lines changed
Lines changed: 2 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
test methods relating to generic function evaluation
3-
the so-called white/black lists
2+
TODO: Existing tests should be moved or deduplicated
3+
Do not add tests here!
44
"""
55

66
from string import ascii_lowercase
@@ -14,11 +14,6 @@
1414
date_range,
1515
)
1616
import pandas._testing as tm
17-
from pandas.core.groupby.base import (
18-
groupby_other_methods,
19-
reduction_kernels,
20-
transformation_kernels,
21-
)
2217

2318
AGG_FUNCTIONS = [
2419
"sum",
@@ -100,131 +95,6 @@ def test_regression_allowlist_methods(raw_frame, op, axis, skipna, sort):
10095
tm.assert_frame_equal(result, expected)
10196

10297

103-
def test_groupby_blocklist(df_letters):
104-
df = df_letters
105-
s = df_letters.floats
106-
107-
blocklist = [
108-
"eval",
109-
"query",
110-
"abs",
111-
"where",
112-
"mask",
113-
"align",
114-
"groupby",
115-
"clip",
116-
"astype",
117-
"at",
118-
"combine",
119-
"consolidate",
120-
"convert_objects",
121-
]
122-
to_methods = [method for method in dir(df) if method.startswith("to_")]
123-
124-
blocklist.extend(to_methods)
125-
126-
for bl in blocklist:
127-
for obj in (df, s):
128-
gb = obj.groupby(df.letters)
129-
130-
# e.g., to_csv
131-
defined_but_not_allowed = (
132-
f"(?:^Cannot.+{repr(bl)}.+'{type(gb).__name__}'.+try "
133-
f"using the 'apply' method$)"
134-
)
135-
136-
# e.g., query, eval
137-
not_defined = (
138-
f"(?:^'{type(gb).__name__}' object has no attribute {repr(bl)}$)"
139-
)
140-
141-
msg = f"{defined_but_not_allowed}|{not_defined}"
142-
143-
with pytest.raises(AttributeError, match=msg):
144-
getattr(gb, bl)
145-
146-
147-
def test_tab_completion(mframe):
148-
grp = mframe.groupby(level="second")
149-
results = {v for v in dir(grp) if not v.startswith("_")}
150-
expected = {
151-
"A",
152-
"B",
153-
"C",
154-
"agg",
155-
"aggregate",
156-
"apply",
157-
"boxplot",
158-
"filter",
159-
"first",
160-
"get_group",
161-
"groups",
162-
"hist",
163-
"indices",
164-
"last",
165-
"max",
166-
"mean",
167-
"median",
168-
"min",
169-
"ngroups",
170-
"nth",
171-
"ohlc",
172-
"plot",
173-
"prod",
174-
"size",
175-
"std",
176-
"sum",
177-
"transform",
178-
"var",
179-
"sem",
180-
"count",
181-
"nunique",
182-
"head",
183-
"describe",
184-
"cummax",
185-
"quantile",
186-
"rank",
187-
"cumprod",
188-
"tail",
189-
"resample",
190-
"cummin",
191-
"fillna",
192-
"cumsum",
193-
"cumcount",
194-
"ngroup",
195-
"all",
196-
"shift",
197-
"skew",
198-
"take",
199-
"pct_change",
200-
"any",
201-
"corr",
202-
"corrwith",
203-
"cov",
204-
"dtypes",
205-
"ndim",
206-
"diff",
207-
"idxmax",
208-
"idxmin",
209-
"ffill",
210-
"bfill",
211-
"rolling",
212-
"expanding",
213-
"pipe",
214-
"sample",
215-
"ewm",
216-
"value_counts",
217-
}
218-
assert results == expected
219-
220-
221-
def test_groupby_function_rename(mframe):
222-
grp = mframe.groupby(level="second")
223-
for name in ["sum", "prod", "min", "max", "first", "last"]:
224-
f = getattr(grp, name)
225-
assert f.__name__ == name
226-
227-
22898
@pytest.mark.parametrize(
22999
"method",
230100
[
@@ -285,47 +155,3 @@ def test_groupby_selection_other_methods(df):
285155
tm.assert_frame_equal(
286156
g.filter(lambda x: len(x) == 3), g_exp.filter(lambda x: len(x) == 3)
287157
)
288-
289-
290-
def test_all_methods_categorized(mframe):
291-
grp = mframe.groupby(mframe.iloc[:, 0])
292-
names = {_ for _ in dir(grp) if not _.startswith("_")} - set(mframe.columns)
293-
new_names = set(names)
294-
new_names -= reduction_kernels
295-
new_names -= transformation_kernels
296-
new_names -= groupby_other_methods
297-
298-
assert not reduction_kernels & transformation_kernels
299-
assert not reduction_kernels & groupby_other_methods
300-
assert not transformation_kernels & groupby_other_methods
301-
302-
# new public method?
303-
if new_names:
304-
msg = f"""
305-
There are uncategorized methods defined on the Grouper class:
306-
{new_names}.
307-
308-
Was a new method recently added?
309-
310-
Every public method On Grouper must appear in exactly one the
311-
following three lists defined in pandas.core.groupby.base:
312-
- `reduction_kernels`
313-
- `transformation_kernels`
314-
- `groupby_other_methods`
315-
see the comments in pandas/core/groupby/base.py for guidance on
316-
how to fix this test.
317-
"""
318-
raise AssertionError(msg)
319-
320-
# removed a public method?
321-
all_categorized = reduction_kernels | transformation_kernels | groupby_other_methods
322-
if names != all_categorized:
323-
msg = f"""
324-
Some methods which are supposed to be on the Grouper class
325-
are missing:
326-
{all_categorized - names}.
327-
328-
They're still defined in one of the lists that live in pandas/core/groupby/base.py.
329-
If you removed a method, you should update them
330-
"""
331-
raise AssertionError(msg)

pandas/tests/groupby/test_api_consistency.py renamed to pandas/tests/groupby/test_api.py

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""
2-
Test the consistency of the groupby API, both internally and with other pandas objects.
2+
Tests of the groupby API, including internal consistency and with other pandas objects.
3+
4+
Tests in this file should only check the existence, names, and arguments of groupby
5+
methods. It should not test the results of any groupby operation.
36
"""
47

58
import inspect
@@ -10,12 +13,135 @@
1013
DataFrame,
1114
Series,
1215
)
16+
from pandas.core.groupby.base import (
17+
groupby_other_methods,
18+
reduction_kernels,
19+
transformation_kernels,
20+
)
1321
from pandas.core.groupby.generic import (
1422
DataFrameGroupBy,
1523
SeriesGroupBy,
1624
)
1725

1826

27+
def test_tab_completion(mframe):
28+
grp = mframe.groupby(level="second")
29+
results = {v for v in dir(grp) if not v.startswith("_")}
30+
expected = {
31+
"A",
32+
"B",
33+
"C",
34+
"agg",
35+
"aggregate",
36+
"apply",
37+
"boxplot",
38+
"filter",
39+
"first",
40+
"get_group",
41+
"groups",
42+
"hist",
43+
"indices",
44+
"last",
45+
"max",
46+
"mean",
47+
"median",
48+
"min",
49+
"ngroups",
50+
"nth",
51+
"ohlc",
52+
"plot",
53+
"prod",
54+
"size",
55+
"std",
56+
"sum",
57+
"transform",
58+
"var",
59+
"sem",
60+
"count",
61+
"nunique",
62+
"head",
63+
"describe",
64+
"cummax",
65+
"quantile",
66+
"rank",
67+
"cumprod",
68+
"tail",
69+
"resample",
70+
"cummin",
71+
"fillna",
72+
"cumsum",
73+
"cumcount",
74+
"ngroup",
75+
"all",
76+
"shift",
77+
"skew",
78+
"take",
79+
"pct_change",
80+
"any",
81+
"corr",
82+
"corrwith",
83+
"cov",
84+
"dtypes",
85+
"ndim",
86+
"diff",
87+
"idxmax",
88+
"idxmin",
89+
"ffill",
90+
"bfill",
91+
"rolling",
92+
"expanding",
93+
"pipe",
94+
"sample",
95+
"ewm",
96+
"value_counts",
97+
}
98+
assert results == expected
99+
100+
101+
def test_all_methods_categorized(mframe):
102+
grp = mframe.groupby(mframe.iloc[:, 0])
103+
names = {_ for _ in dir(grp) if not _.startswith("_")} - set(mframe.columns)
104+
new_names = set(names)
105+
new_names -= reduction_kernels
106+
new_names -= transformation_kernels
107+
new_names -= groupby_other_methods
108+
109+
assert not reduction_kernels & transformation_kernels
110+
assert not reduction_kernels & groupby_other_methods
111+
assert not transformation_kernels & groupby_other_methods
112+
113+
# new public method?
114+
if new_names:
115+
msg = f"""
116+
There are uncategorized methods defined on the Grouper class:
117+
{new_names}.
118+
119+
Was a new method recently added?
120+
121+
Every public method On Grouper must appear in exactly one the
122+
following three lists defined in pandas.core.groupby.base:
123+
- `reduction_kernels`
124+
- `transformation_kernels`
125+
- `groupby_other_methods`
126+
see the comments in pandas/core/groupby/base.py for guidance on
127+
how to fix this test.
128+
"""
129+
raise AssertionError(msg)
130+
131+
# removed a public method?
132+
all_categorized = reduction_kernels | transformation_kernels | groupby_other_methods
133+
if names != all_categorized:
134+
msg = f"""
135+
Some methods which are supposed to be on the Grouper class
136+
are missing:
137+
{all_categorized - names}.
138+
139+
They're still defined in one of the lists that live in pandas/core/groupby/base.py.
140+
If you removed a method, you should update them
141+
"""
142+
raise AssertionError(msg)
143+
144+
19145
def test_frame_consistency(groupby_func):
20146
# GH#48028
21147
if groupby_func in ("first", "last"):

0 commit comments

Comments
 (0)