Skip to content

Commit 8661548

Browse files
authored
TST: Consolidate tests that raise in groupby (#50749)
1 parent c95b9e2 commit 8661548

File tree

5 files changed

+467
-67
lines changed

5 files changed

+467
-67
lines changed

pandas/tests/groupby/aggregate/test_cython.py

-5
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,8 @@ def test_cython_agg_nothing_to_agg():
9696
with pytest.raises(TypeError, match=msg):
9797
frame.groupby("a")["b"].mean(numeric_only=True)
9898

99-
with pytest.raises(TypeError, match="Could not convert (foo|bar)*"):
100-
frame.groupby("a")["b"].mean()
101-
10299
frame = DataFrame({"a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25})
103100

104-
with pytest.raises(TypeError, match="Could not convert"):
105-
frame[["b"]].groupby(frame["a"]).mean()
106101
result = frame[["b"]].groupby(frame["a"]).mean(numeric_only=True)
107102
expected = DataFrame(
108103
[], index=frame["a"].sort_values().drop_duplicates(), columns=[]

pandas/tests/groupby/test_categorical.py

-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ def test_basic(): # TODO: split this test
103103
gb = df.groupby("A", observed=False)
104104
exp_idx = CategoricalIndex(["a", "b", "z"], name="A", ordered=True)
105105
expected = DataFrame({"values": Series([3, 7, 0], index=exp_idx)})
106-
msg = "category type does not support sum operations"
107-
with pytest.raises(TypeError, match=msg):
108-
gb.sum()
109106
result = gb.sum(numeric_only=True)
110107
tm.assert_frame_equal(result, expected)
111108

pandas/tests/groupby/test_groupby.py

-28
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,6 @@ def test_multi_func(df):
465465
col2 = df["B"]
466466

467467
grouped = df.groupby([col1.get, col2.get])
468-
with pytest.raises(TypeError, match="Could not convert"):
469-
grouped.mean()
470468
agged = grouped.mean(numeric_only=True)
471469
expected = df.groupby(["A", "B"]).mean()
472470

@@ -665,17 +663,11 @@ def test_groupby_as_index_agg(df):
665663

666664
# single-key
667665

668-
with pytest.raises(TypeError, match="Could not convert"):
669-
grouped.agg(np.mean)
670666
result = grouped[["C", "D"]].agg(np.mean)
671-
with pytest.raises(TypeError, match="Could not convert"):
672-
grouped.mean()
673667
expected = grouped.mean(numeric_only=True)
674668
tm.assert_frame_equal(result, expected)
675669

676670
result2 = grouped.agg({"C": np.mean, "D": np.sum})
677-
with pytest.raises(TypeError, match="Could not convert"):
678-
grouped.mean()
679671
expected2 = grouped.mean(numeric_only=True)
680672
expected2["D"] = grouped.sum()["D"]
681673
tm.assert_frame_equal(result2, expected2)
@@ -793,11 +785,7 @@ def test_groupby_as_index_cython(df):
793785

794786
# single-key
795787
grouped = data.groupby("A", as_index=False)
796-
with pytest.raises(TypeError, match="Could not convert"):
797-
grouped.mean()
798788
result = grouped.mean(numeric_only=True)
799-
with pytest.raises(TypeError, match="Could not convert"):
800-
data.groupby(["A"]).mean()
801789
expected = data.groupby(["A"]).mean(numeric_only=True)
802790
expected.insert(0, "A", expected.index)
803791
expected.index = RangeIndex(len(expected))
@@ -960,11 +948,7 @@ def test_empty_groups_corner(mframe):
960948
)
961949

962950
grouped = df.groupby(["k1", "k2"])
963-
with pytest.raises(TypeError, match="Could not convert"):
964-
grouped.agg(np.mean)
965951
result = grouped[["v1", "v2"]].agg(np.mean)
966-
with pytest.raises(TypeError, match="Could not convert"):
967-
grouped.mean()
968952
expected = grouped.mean(numeric_only=True)
969953
tm.assert_frame_equal(result, expected)
970954

@@ -1148,8 +1132,6 @@ def test_groupby_with_hier_columns():
11481132
# add a nuisance column
11491133
sorted_columns, _ = columns.sortlevel(0)
11501134
df["A", "foo"] = "bar"
1151-
with pytest.raises(TypeError, match="Could not convert"):
1152-
df.groupby(level=0).mean()
11531135
result = df.groupby(level=0).mean(numeric_only=True)
11541136
tm.assert_index_equal(result.columns, df.columns[:-1])
11551137

@@ -1183,11 +1165,7 @@ def test_groupby_wrong_multi_labels():
11831165

11841166

11851167
def test_groupby_series_with_name(df):
1186-
with pytest.raises(TypeError, match="Could not convert"):
1187-
df.groupby(df["A"]).mean()
11881168
result = df.groupby(df["A"]).mean(numeric_only=True)
1189-
with pytest.raises(TypeError, match="Could not convert"):
1190-
df.groupby(df["A"], as_index=False).mean()
11911169
result2 = df.groupby(df["A"], as_index=False).mean(numeric_only=True)
11921170
assert result.index.name == "A"
11931171
assert "A" in result2
@@ -1337,11 +1315,7 @@ def test_groupby_unit64_float_conversion():
13371315

13381316

13391317
def test_groupby_list_infer_array_like(df):
1340-
with pytest.raises(TypeError, match="Could not convert"):
1341-
df.groupby(list(df["A"])).mean()
13421318
result = df.groupby(list(df["A"])).mean(numeric_only=True)
1343-
with pytest.raises(TypeError, match="Could not convert"):
1344-
df.groupby(df["A"]).mean()
13451319
expected = df.groupby(df["A"]).mean(numeric_only=True)
13461320
tm.assert_frame_equal(result, expected, check_names=False)
13471321

@@ -1455,8 +1429,6 @@ def test_groupby_2d_malformed():
14551429
d["zeros"] = [0, 0]
14561430
d["ones"] = [1, 1]
14571431
d["label"] = ["l1", "l2"]
1458-
with pytest.raises(TypeError, match="Could not convert"):
1459-
d.groupby(["group"]).mean()
14601432
tmp = d.groupby(["group"]).mean(numeric_only=True)
14611433
res_values = np.array([[0.0, 1.0], [0.0, 1.0]])
14621434
tm.assert_index_equal(tmp.columns, Index(["zeros", "ones"]))

pandas/tests/groupby/test_index_as_string.py

-5
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ def series():
4848
)
4949
def test_grouper_index_level_as_string(frame, key_strs, groupers):
5050
if "B" not in key_strs or "outer" in frame.columns:
51-
with pytest.raises(TypeError, match="Could not convert"):
52-
frame.groupby(key_strs).mean()
5351
result = frame.groupby(key_strs).mean(numeric_only=True)
54-
55-
with pytest.raises(TypeError, match="Could not convert"):
56-
frame.groupby(groupers).mean()
5752
expected = frame.groupby(groupers).mean(numeric_only=True)
5853
else:
5954
result = frame.groupby(key_strs).mean()

0 commit comments

Comments
 (0)