Skip to content

Commit 094f630

Browse files
authored
TST: Consolidate groupby any, all tests (#40811)
1 parent c2f1271 commit 094f630

File tree

3 files changed

+70
-55
lines changed

3 files changed

+70
-55
lines changed

pandas/tests/groupby/test_any_all.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import builtins
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import (
7+
DataFrame,
8+
Index,
9+
isna,
10+
)
11+
import pandas._testing as tm
12+
13+
14+
@pytest.mark.parametrize("agg_func", ["any", "all"])
15+
@pytest.mark.parametrize("skipna", [True, False])
16+
@pytest.mark.parametrize(
17+
"vals",
18+
[
19+
["foo", "bar", "baz"],
20+
["foo", "", ""],
21+
["", "", ""],
22+
[1, 2, 3],
23+
[1, 0, 0],
24+
[0, 0, 0],
25+
[1.0, 2.0, 3.0],
26+
[1.0, 0.0, 0.0],
27+
[0.0, 0.0, 0.0],
28+
[True, True, True],
29+
[True, False, False],
30+
[False, False, False],
31+
[np.nan, np.nan, np.nan],
32+
],
33+
)
34+
def test_groupby_bool_aggs(agg_func, skipna, vals):
35+
df = DataFrame({"key": ["a"] * 3 + ["b"] * 3, "val": vals * 2})
36+
37+
# Figure out expectation using Python builtin
38+
exp = getattr(builtins, agg_func)(vals)
39+
40+
# edge case for missing data with skipna and 'any'
41+
if skipna and all(isna(vals)) and agg_func == "any":
42+
exp = False
43+
44+
exp_df = DataFrame([exp] * 2, columns=["val"], index=Index(["a", "b"], name="key"))
45+
result = getattr(df.groupby("key"), agg_func)(skipna=skipna)
46+
tm.assert_frame_equal(result, exp_df)
47+
48+
49+
def test_any():
50+
df = DataFrame(
51+
[[1, 2, "foo"], [1, np.nan, "bar"], [3, np.nan, "baz"]],
52+
columns=["A", "B", "C"],
53+
)
54+
expected = DataFrame(
55+
[[True, True], [False, True]], columns=["B", "C"], index=[1, 3]
56+
)
57+
expected.index.name = "A"
58+
result = df.groupby("A").any()
59+
tm.assert_frame_equal(result, expected)
60+
61+
62+
@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
63+
def test_bool_aggs_dup_column_labels(bool_agg_func):
64+
# 21668
65+
df = DataFrame([[True, True]], columns=["a", "a"])
66+
grp_by = df.groupby([0])
67+
result = getattr(grp_by, bool_agg_func)()
68+
69+
expected = df
70+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_function.py

-44
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Series,
1515
Timestamp,
1616
date_range,
17-
isna,
1817
)
1918
import pandas._testing as tm
2019
import pandas.core.nanops as nanops
@@ -41,41 +40,6 @@ def numpy_dtypes_for_minmax(request):
4140
return (dtype, min_val, max_val)
4241

4342

44-
@pytest.mark.parametrize("agg_func", ["any", "all"])
45-
@pytest.mark.parametrize("skipna", [True, False])
46-
@pytest.mark.parametrize(
47-
"vals",
48-
[
49-
["foo", "bar", "baz"],
50-
["foo", "", ""],
51-
["", "", ""],
52-
[1, 2, 3],
53-
[1, 0, 0],
54-
[0, 0, 0],
55-
[1.0, 2.0, 3.0],
56-
[1.0, 0.0, 0.0],
57-
[0.0, 0.0, 0.0],
58-
[True, True, True],
59-
[True, False, False],
60-
[False, False, False],
61-
[np.nan, np.nan, np.nan],
62-
],
63-
)
64-
def test_groupby_bool_aggs(agg_func, skipna, vals):
65-
df = DataFrame({"key": ["a"] * 3 + ["b"] * 3, "val": vals * 2})
66-
67-
# Figure out expectation using Python builtin
68-
exp = getattr(builtins, agg_func)(vals)
69-
70-
# edge case for missing data with skipna and 'any'
71-
if skipna and all(isna(vals)) and agg_func == "any":
72-
exp = False
73-
74-
exp_df = DataFrame([exp] * 2, columns=["val"], index=Index(["a", "b"], name="key"))
75-
result = getattr(df.groupby("key"), agg_func)(skipna=skipna)
76-
tm.assert_frame_equal(result, exp_df)
77-
78-
7943
def test_max_min_non_numeric():
8044
# #2700
8145
aa = DataFrame({"nn": [11, 11, 22, 22], "ii": [1, 2, 3, 4], "ss": 4 * ["mama"]})
@@ -344,14 +308,6 @@ def test_idxmin(self, gb):
344308
result = gb.idxmin()
345309
tm.assert_frame_equal(result, expected)
346310

347-
def test_any(self, gb):
348-
expected = DataFrame(
349-
[[True, True], [False, True]], columns=["B", "C"], index=[1, 3]
350-
)
351-
expected.index.name = "A"
352-
result = gb.any()
353-
tm.assert_frame_equal(result, expected)
354-
355311
def test_mad(self, gb, gni):
356312
# mad
357313
expected = DataFrame([[0], [np.nan]], columns=["B"], index=[1, 3])

pandas/tests/groupby/test_groupby.py

-11
Original file line numberDiff line numberDiff line change
@@ -1978,17 +1978,6 @@ def test_groupby_duplicate_index():
19781978
tm.assert_series_equal(result, expected)
19791979

19801980

1981-
@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
1982-
def test_bool_aggs_dup_column_labels(bool_agg_func):
1983-
# 21668
1984-
df = DataFrame([[True, True]], columns=["a", "a"])
1985-
grp_by = df.groupby([0])
1986-
result = getattr(grp_by, bool_agg_func)()
1987-
1988-
expected = df
1989-
tm.assert_frame_equal(result, expected)
1990-
1991-
19921981
@pytest.mark.parametrize(
19931982
"idx", [Index(["a", "a"]), MultiIndex.from_tuples((("a", "a"), ("a", "a")))]
19941983
)

0 commit comments

Comments
 (0)