Skip to content

Commit 2e1206e

Browse files
authored
TST: Consolidate tests that raise in groupby (#50404)
1 parent 9993ec4 commit 2e1206e

File tree

6 files changed

+178
-62
lines changed

6 files changed

+178
-62
lines changed

pandas/tests/groupby/test_function.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ def test_averages(self, df, method):
166166
],
167167
)
168168

169-
with pytest.raises(TypeError, match="[Cc]ould not convert"):
170-
getattr(gb, method)()
171169
result = getattr(gb, method)(numeric_only=True)
172170
tm.assert_frame_equal(result.reindex_like(expected), expected)
173171

@@ -317,21 +315,6 @@ def gni(self, df):
317315
gni = df.groupby("A", as_index=False)
318316
return gni
319317

320-
# TODO: non-unique columns, as_index=False
321-
def test_idxmax_nuisance_raises(self, gb):
322-
# GH#5610, GH#41480
323-
expected = DataFrame([[0.0], [np.nan]], columns=["B"], index=[1, 3])
324-
expected.index.name = "A"
325-
with pytest.raises(TypeError, match="not allowed for this dtype"):
326-
gb.idxmax()
327-
328-
def test_idxmin_nuisance_raises(self, gb):
329-
# GH#5610, GH#41480
330-
expected = DataFrame([[0.0], [np.nan]], columns=["B"], index=[1, 3])
331-
expected.index.name = "A"
332-
with pytest.raises(TypeError, match="not allowed for this dtype"):
333-
gb.idxmin()
334-
335318
def test_describe(self, df, gb, gni):
336319
# describe
337320
expected_index = Index([1, 3], name="A")

pandas/tests/groupby/test_groupby.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,19 +433,12 @@ def test_frame_groupby_columns(tsframe):
433433
def test_frame_set_name_single(df):
434434
grouped = df.groupby("A")
435435

436-
msg = "The default value of numeric_only"
437-
with pytest.raises(TypeError, match="Could not convert"):
438-
grouped.mean()
439436
result = grouped.mean(numeric_only=True)
440437
assert result.index.name == "A"
441438

442-
with pytest.raises(TypeError, match="Could not convert"):
443-
df.groupby("A", as_index=False).mean()
444439
result = df.groupby("A", as_index=False).mean(numeric_only=True)
445440
assert result.index.name != "A"
446441

447-
with pytest.raises(TypeError, match="Could not convert"):
448-
grouped.agg(np.mean)
449442
result = grouped[["C", "D"]].agg(np.mean)
450443
assert result.index.name == "A"
451444

pandas/tests/groupby/test_grouping.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ def test_column_select_via_attr(self, df):
5555
tm.assert_series_equal(result, expected)
5656

5757
df["mean"] = 1.5
58-
with pytest.raises(TypeError, match="Could not convert"):
59-
df.groupby("A").mean()
6058
result = df.groupby("A").mean(numeric_only=True)
61-
with pytest.raises(TypeError, match="Could not convert"):
62-
df.groupby("A").agg(np.mean)
6359
expected = df.groupby("A")[["C", "D", "mean"]].agg(np.mean)
6460
tm.assert_frame_equal(result, expected)
6561

@@ -289,8 +285,6 @@ def test_grouper_column_and_index(self):
289285
result = df_multi.groupby(["B", pd.Grouper(level="inner")]).mean(
290286
numeric_only=True
291287
)
292-
with pytest.raises(TypeError, match="Could not convert"):
293-
df_multi.reset_index().groupby(["B", "inner"]).mean()
294288
expected = (
295289
df_multi.reset_index().groupby(["B", "inner"]).mean(numeric_only=True)
296290
)
@@ -300,8 +294,6 @@ def test_grouper_column_and_index(self):
300294
result = df_multi.groupby([pd.Grouper(level="inner"), "B"]).mean(
301295
numeric_only=True
302296
)
303-
with pytest.raises(TypeError, match="Could not convert"):
304-
df_multi.reset_index().groupby(["inner", "B"]).mean()
305297
expected = (
306298
df_multi.reset_index().groupby(["inner", "B"]).mean(numeric_only=True)
307299
)
@@ -310,26 +302,18 @@ def test_grouper_column_and_index(self):
310302
# Grouping a single-index frame by a column and the index should
311303
# be equivalent to resetting the index and grouping by two columns
312304
df_single = df_multi.reset_index("outer")
313-
with pytest.raises(TypeError, match="Could not convert"):
314-
df_single.groupby(["B", pd.Grouper(level="inner")]).mean()
315305
result = df_single.groupby(["B", pd.Grouper(level="inner")]).mean(
316306
numeric_only=True
317307
)
318-
with pytest.raises(TypeError, match="Could not convert"):
319-
df_single.reset_index().groupby(["B", "inner"]).mean()
320308
expected = (
321309
df_single.reset_index().groupby(["B", "inner"]).mean(numeric_only=True)
322310
)
323311
tm.assert_frame_equal(result, expected)
324312

325313
# Test the reverse grouping order
326-
with pytest.raises(TypeError, match="Could not convert"):
327-
df_single.groupby([pd.Grouper(level="inner"), "B"]).mean()
328314
result = df_single.groupby([pd.Grouper(level="inner"), "B"]).mean(
329315
numeric_only=True
330316
)
331-
with pytest.raises(TypeError, match="Could not convert"):
332-
df_single.reset_index().groupby(["inner", "B"]).mean()
333317
expected = (
334318
df_single.reset_index().groupby(["inner", "B"]).mean(numeric_only=True)
335319
)
@@ -406,11 +390,7 @@ def test_empty_groups(self, df):
406390
def test_groupby_grouper(self, df):
407391
grouped = df.groupby("A")
408392

409-
with pytest.raises(TypeError, match="Could not convert"):
410-
df.groupby(grouped.grouper).mean()
411393
result = df.groupby(grouped.grouper).mean(numeric_only=True)
412-
with pytest.raises(TypeError, match="Could not convert"):
413-
grouped.mean()
414394
expected = grouped.mean(numeric_only=True)
415395
tm.assert_frame_equal(result, expected)
416396

pandas/tests/groupby/test_min_max.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ def test_max_min_object_multiple_columns(using_array_manager):
4747

4848
gb = df.groupby("A")
4949

50-
with pytest.raises(TypeError, match="not supported between instances"):
51-
gb.max(numeric_only=False)
5250
result = gb[["C"]].max()
5351
# "max" is valid for column "C" but not for "B"
5452
ei = Index([1, 2, 3], name="A")
5553
expected = DataFrame({"C": ["b", "d", "e"]}, index=ei)
5654
tm.assert_frame_equal(result, expected)
5755

58-
with pytest.raises(TypeError, match="not supported between instances"):
59-
gb.max(numeric_only=False)
6056
result = gb[["C"]].min()
6157
# "min" is valid for column "C" but not for "B"
6258
ei = Index([1, 2, 3], name="A")

pandas/tests/groupby/test_raises.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Only tests that raise an error and have no better location should go here.
2+
# Tests for specific groupby methods should go in their respective
3+
# test file.
4+
5+
import datetime
6+
7+
import pytest
8+
9+
from pandas import DataFrame
10+
from pandas.tests.groupby import get_groupby_method_args
11+
12+
13+
@pytest.mark.parametrize("how", ["method", "agg", "transform"])
14+
def test_groupby_raises_string(how, groupby_func, as_index, sort):
15+
df = DataFrame(
16+
{
17+
"a": [1, 1, 1, 2, 2],
18+
"b": range(5),
19+
"c": list("xyzwt"),
20+
}
21+
)
22+
args = get_groupby_method_args(groupby_func, df)
23+
gb = df.groupby("a", as_index=as_index, sort=sort)
24+
25+
klass, msg = {
26+
"all": (None, ""),
27+
"any": (None, ""),
28+
"bfill": (None, ""),
29+
"corrwith": (TypeError, "Could not convert"),
30+
"count": (None, ""),
31+
"cumcount": (None, ""),
32+
"cummax": (NotImplementedError, "function is not implemented for this dtype"),
33+
"cummin": (NotImplementedError, "function is not implemented for this dtype"),
34+
"cumprod": (NotImplementedError, "function is not implemented for this dtype"),
35+
"cumsum": (NotImplementedError, "function is not implemented for this dtype"),
36+
"diff": (TypeError, "unsupported operand type"),
37+
"ffill": (None, ""),
38+
"fillna": (None, ""),
39+
"first": (None, ""),
40+
"idxmax": (TypeError, "'argmax' not allowed for this dtype"),
41+
"idxmin": (TypeError, "'argmin' not allowed for this dtype"),
42+
"last": (None, ""),
43+
"max": (None, ""),
44+
"mean": (TypeError, "Could not convert xyz to numeric"),
45+
"median": (TypeError, "could not convert string to float"),
46+
"min": (None, ""),
47+
"ngroup": (None, ""),
48+
"nunique": (None, ""),
49+
"pct_change": (TypeError, "unsupported operand type"),
50+
"prod": (TypeError, "can't multiply sequence by non-int of type 'str'"),
51+
"quantile": (TypeError, "cannot be performed against 'object' dtypes!"),
52+
"rank": (None, ""),
53+
"sem": (ValueError, "could not convert string to float"),
54+
"shift": (None, ""),
55+
"size": (None, ""),
56+
"skew": (TypeError, "could not convert string to float"),
57+
"std": (ValueError, "could not convert string to float"),
58+
"sum": (None, ""),
59+
"var": (TypeError, "could not convert string to float"),
60+
}[groupby_func]
61+
62+
if klass is None:
63+
if how == "method":
64+
getattr(gb, groupby_func)(*args)
65+
elif how == "agg":
66+
gb.agg(groupby_func, *args)
67+
else:
68+
gb.transform(groupby_func, *args)
69+
else:
70+
with pytest.raises(klass, match=msg):
71+
if how == "method":
72+
getattr(gb, groupby_func)(*args)
73+
elif how == "agg":
74+
gb.agg(groupby_func, *args)
75+
else:
76+
gb.transform(groupby_func, *args)
77+
78+
79+
@pytest.mark.parametrize("how", ["agg", "transform"])
80+
def test_groupby_raises_string_udf(how):
81+
df = DataFrame(
82+
{
83+
"a": [1, 1, 1, 2, 2],
84+
"b": range(5),
85+
"c": list("xyzwt"),
86+
}
87+
)
88+
gb = df.groupby("a")
89+
90+
def func(x):
91+
raise TypeError("Test error message")
92+
93+
with pytest.raises(TypeError, match="Test error message"):
94+
getattr(gb, how)(func)
95+
96+
97+
@pytest.mark.parametrize("how", ["method", "agg", "transform"])
98+
def test_groupby_raises_datetime(how, groupby_func, as_index, sort):
99+
df = DataFrame(
100+
{
101+
"a": [1, 1, 1, 2, 2],
102+
"b": range(5),
103+
"c": datetime.datetime(2005, 1, 1, 10, 30, 23, 540000),
104+
}
105+
)
106+
args = get_groupby_method_args(groupby_func, df)
107+
gb = df.groupby("a", as_index=as_index, sort=sort)
108+
109+
klass, msg = {
110+
"all": (None, ""),
111+
"any": (None, ""),
112+
"bfill": (None, ""),
113+
"corrwith": (TypeError, "cannot perform __mul__ with this index type"),
114+
"count": (None, ""),
115+
"cumcount": (None, ""),
116+
"cummax": (None, ""),
117+
"cummin": (None, ""),
118+
"cumprod": (TypeError, "datetime64 type does not support cumprod operations"),
119+
"cumsum": (TypeError, "datetime64 type does not support cumsum operations"),
120+
"diff": (None, ""),
121+
"ffill": (None, ""),
122+
"fillna": (None, ""),
123+
"first": (None, ""),
124+
"idxmax": (None, ""),
125+
"idxmin": (None, ""),
126+
"last": (None, ""),
127+
"max": (None, ""),
128+
"mean": (None, ""),
129+
"median": (None, ""),
130+
"min": (None, ""),
131+
"ngroup": (None, ""),
132+
"nunique": (None, ""),
133+
"pct_change": (TypeError, "cannot perform __truediv__ with this index type"),
134+
"prod": (TypeError, "datetime64 type does not support prod"),
135+
"quantile": (None, ""),
136+
"rank": (None, ""),
137+
"sem": (TypeError, "Cannot cast DatetimeArray to dtype float64"),
138+
"shift": (None, ""),
139+
"size": (None, ""),
140+
"skew": (TypeError, r"dtype datetime64\[ns\] does not support reduction"),
141+
"std": (TypeError, "Cannot cast DatetimeArray to dtype float64"),
142+
"sum": (TypeError, "datetime64 type does not support sum operations"),
143+
"var": (None, ""),
144+
}[groupby_func]
145+
146+
if klass is None:
147+
if how == "method":
148+
getattr(gb, groupby_func)(*args)
149+
elif how == "agg":
150+
gb.agg(groupby_func, *args)
151+
else:
152+
gb.transform(groupby_func, *args)
153+
else:
154+
with pytest.raises(klass, match=msg):
155+
if how == "method":
156+
getattr(gb, groupby_func)(*args)
157+
elif how == "agg":
158+
gb.agg(groupby_func, *args)
159+
else:
160+
gb.transform(groupby_func, *args)
161+
162+
163+
@pytest.mark.parametrize("how", ["agg", "transform"])
164+
def test_groupby_raises_datetime_udf(how):
165+
df = DataFrame(
166+
{
167+
"a": [1, 1, 1, 2, 2],
168+
"b": range(5),
169+
"c": datetime.datetime(2005, 1, 1, 10, 30, 23, 540000),
170+
}
171+
)
172+
gb = df.groupby("a")
173+
174+
def func(x):
175+
raise TypeError("Test error message")
176+
177+
with pytest.raises(TypeError, match="Test error message"):
178+
getattr(gb, how)(func)

pandas/tests/groupby/transform/test_transform.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,7 @@ def test_transform_nuisance_raises(df):
426426

427427

428428
def test_transform_function_aliases(df):
429-
with pytest.raises(TypeError, match="Could not convert"):
430-
df.groupby("A").transform("mean")
431429
result = df.groupby("A").transform("mean", numeric_only=True)
432-
with pytest.raises(TypeError, match="Could not convert"):
433-
df.groupby("A").transform(np.mean)
434430
expected = df.groupby("A")[["C", "D"]].transform(np.mean)
435431
tm.assert_frame_equal(result, expected)
436432

@@ -508,8 +504,6 @@ def test_groupby_transform_with_int():
508504
}
509505
)
510506
with np.errstate(all="ignore"):
511-
with pytest.raises(TypeError, match="Could not convert"):
512-
df.groupby("A").transform(lambda x: (x - x.mean()) / x.std())
513507
result = df.groupby("A")[["B", "C"]].transform(
514508
lambda x: (x - x.mean()) / x.std()
515509
)
@@ -554,8 +548,6 @@ def test_groupby_transform_with_int():
554548
tm.assert_frame_equal(result, expected)
555549

556550
# int doesn't get downcasted
557-
with pytest.raises(TypeError, match="unsupported operand type"):
558-
df.groupby("A").transform(lambda x: x * 2 / 2)
559551
result = df.groupby("A")[["B", "C"]].transform(lambda x: x * 2 / 2)
560552
expected = DataFrame({"B": 1.0, "C": [2.0, 3.0, 4.0, 10.0, 5.0, -1.0]})
561553
tm.assert_frame_equal(result, expected)
@@ -748,14 +740,8 @@ def test_cython_transform_frame(op, args, targop):
748740

749741
expected = expected.sort_index(axis=1)
750742

751-
if op != "shift":
752-
with pytest.raises(TypeError, match="datetime64 type does not support"):
753-
gb.transform(op, *args).sort_index(axis=1)
754743
result = gb[expected.columns].transform(op, *args).sort_index(axis=1)
755744
tm.assert_frame_equal(result, expected)
756-
if op != "shift":
757-
with pytest.raises(TypeError, match="datetime64 type does not support"):
758-
getattr(gb, op)(*args).sort_index(axis=1)
759745
result = getattr(gb[expected.columns], op)(*args).sort_index(axis=1)
760746
tm.assert_frame_equal(result, expected)
761747
# individual columns

0 commit comments

Comments
 (0)