Skip to content

Commit 5d9f689

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
STY: use pytest.raises context syntax (groupby) (pandas-dev#24828)
1 parent 7a6306c commit 5d9f689

File tree

8 files changed

+141
-66
lines changed

8 files changed

+141
-66
lines changed

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def get_converter(s):
438438
return [self.indices[name] for name in names]
439439
except KeyError:
440440
# turns out it wasn't a tuple
441-
msg = ("must supply a a same-length tuple to get_group"
441+
msg = ("must supply a same-length tuple to get_group"
442442
" with multiple grouping keys")
443443
raise ValueError(msg)
444444

pandas/core/groupby/grouper.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
464464
raise ValueError('level name {} is not the name of the '
465465
'index'.format(level))
466466
elif level > 0 or level < -1:
467-
raise ValueError('level > 0 or level < -1 only valid with '
468-
' MultiIndex')
467+
raise ValueError(
468+
'level > 0 or level < -1 only valid with MultiIndex')
469469

470470
# NOTE: `group_axis` and `group_axis.get_level_values(level)`
471471
# are same in this section.

pandas/tests/groupby/test_bin_groupby.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,18 @@ def test_generate_bins(self):
7373
bins = func(values, binner, closed='right')
7474
assert ((bins == np.array([3, 6])).all())
7575

76-
pytest.raises(ValueError, generate_bins_generic, values, [],
77-
'right')
78-
pytest.raises(ValueError, generate_bins_generic, values[:0],
79-
binner, 'right')
80-
81-
pytest.raises(ValueError, generate_bins_generic, values, [4],
82-
'right')
83-
pytest.raises(ValueError, generate_bins_generic, values, [-3, -1],
84-
'right')
76+
msg = "Invalid length for values or for binner"
77+
with pytest.raises(ValueError, match=msg):
78+
generate_bins_generic(values, [], 'right')
79+
with pytest.raises(ValueError, match=msg):
80+
generate_bins_generic(values[:0], binner, 'right')
81+
82+
msg = "Values falls before first bin"
83+
with pytest.raises(ValueError, match=msg):
84+
generate_bins_generic(values, [4], 'right')
85+
msg = "Values falls after last bin"
86+
with pytest.raises(ValueError, match=msg):
87+
generate_bins_generic(values, [-3, -1], 'right')
8588

8689

8790
def test_group_ohlc():

pandas/tests/groupby/test_filters.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ def raise_if_sum_is_zero(x):
116116
s = pd.Series([-1, 0, 1, 2])
117117
grouper = s.apply(lambda x: x % 2)
118118
grouped = s.groupby(grouper)
119-
pytest.raises(TypeError,
120-
lambda: grouped.filter(raise_if_sum_is_zero))
119+
msg = "the filter must return a boolean result"
120+
with pytest.raises(TypeError, match=msg):
121+
grouped.filter(raise_if_sum_is_zero)
121122

122123

123124
def test_filter_with_axis_in_groupby():
@@ -140,16 +141,28 @@ def test_filter_bad_shapes():
140141
g_s = s.groupby(s)
141142

142143
f = lambda x: x
143-
pytest.raises(TypeError, lambda: g_df.filter(f))
144-
pytest.raises(TypeError, lambda: g_s.filter(f))
144+
msg = "filter function returned a DataFrame, but expected a scalar bool"
145+
with pytest.raises(TypeError, match=msg):
146+
g_df.filter(f)
147+
msg = "the filter must return a boolean result"
148+
with pytest.raises(TypeError, match=msg):
149+
g_s.filter(f)
145150

146151
f = lambda x: x == 1
147-
pytest.raises(TypeError, lambda: g_df.filter(f))
148-
pytest.raises(TypeError, lambda: g_s.filter(f))
152+
msg = "filter function returned a DataFrame, but expected a scalar bool"
153+
with pytest.raises(TypeError, match=msg):
154+
g_df.filter(f)
155+
msg = "the filter must return a boolean result"
156+
with pytest.raises(TypeError, match=msg):
157+
g_s.filter(f)
149158

150159
f = lambda x: np.outer(x, x)
151-
pytest.raises(TypeError, lambda: g_df.filter(f))
152-
pytest.raises(TypeError, lambda: g_s.filter(f))
160+
msg = "can't multiply sequence by non-int of type 'str'"
161+
with pytest.raises(TypeError, match=msg):
162+
g_df.filter(f)
163+
msg = "the filter must return a boolean result"
164+
with pytest.raises(TypeError, match=msg):
165+
g_s.filter(f)
153166

154167

155168
def test_filter_nan_is_false():

pandas/tests/groupby/test_function.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,11 @@ def test_frame_describe_tupleindex():
761761
'z': [100, 200, 300, 400, 500] * 3})
762762
df1['k'] = [(0, 0, 1), (0, 1, 0), (1, 0, 0)] * 5
763763
df2 = df1.rename(columns={'k': 'key'})
764-
pytest.raises(ValueError, lambda: df1.groupby('k').describe())
765-
pytest.raises(ValueError, lambda: df2.groupby('key').describe())
764+
msg = "Names should be list-like for a MultiIndex"
765+
with pytest.raises(ValueError, match=msg):
766+
df1.groupby('k').describe()
767+
with pytest.raises(ValueError, match=msg):
768+
df2.groupby('key').describe()
766769

767770

768771
def test_frame_describe_unstacked_format():

pandas/tests/groupby/test_groupby.py

+61-27
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def test_basic(dtype):
7171
assert agged[1] == 21
7272

7373
# corner cases
74-
pytest.raises(Exception, grouped.aggregate, lambda x: x * 2)
74+
msg = "Must produce aggregated value"
75+
# exception raised is type Exception
76+
with pytest.raises(Exception, match=msg):
77+
grouped.aggregate(lambda x: x * 2)
7578

7679

7780
def test_groupby_nonobject_dtype(mframe, df_mixed_floats):
@@ -330,12 +333,17 @@ def f3(x):
330333
assert_frame_equal(result1, result2)
331334

332335
# should fail (not the same number of levels)
333-
pytest.raises(AssertionError, df.groupby('a').apply, f2)
334-
pytest.raises(AssertionError, df2.groupby('a').apply, f2)
336+
msg = "Cannot concat indices that do not have the same number of levels"
337+
with pytest.raises(AssertionError, match=msg):
338+
df.groupby('a').apply(f2)
339+
with pytest.raises(AssertionError, match=msg):
340+
df2.groupby('a').apply(f2)
335341

336342
# should fail (incorrect shape)
337-
pytest.raises(AssertionError, df.groupby('a').apply, f3)
338-
pytest.raises(AssertionError, df2.groupby('a').apply, f3)
343+
with pytest.raises(AssertionError, match=msg):
344+
df.groupby('a').apply(f3)
345+
with pytest.raises(AssertionError, match=msg):
346+
df2.groupby('a').apply(f3)
339347

340348

341349
def test_attr_wrapper(ts):
@@ -356,7 +364,9 @@ def test_attr_wrapper(ts):
356364
expected = grouped.agg(lambda x: x.dtype)
357365

358366
# make sure raises error
359-
pytest.raises(AttributeError, getattr, grouped, 'foo')
367+
msg = "'SeriesGroupBy' object has no attribute 'foo'"
368+
with pytest.raises(AttributeError, match=msg):
369+
getattr(grouped, 'foo')
360370

361371

362372
def test_frame_groupby(tsframe):
@@ -664,11 +674,13 @@ def test_groupby_as_index_series_scalar(df):
664674

665675

666676
def test_groupby_as_index_corner(df, ts):
667-
pytest.raises(TypeError, ts.groupby, lambda x: x.weekday(),
668-
as_index=False)
677+
msg = "as_index=False only valid with DataFrame"
678+
with pytest.raises(TypeError, match=msg):
679+
ts.groupby(lambda x: x.weekday(), as_index=False)
669680

670-
pytest.raises(ValueError, df.groupby, lambda x: x.lower(),
671-
as_index=False, axis=1)
681+
msg = "as_index=False only valid for axis=0"
682+
with pytest.raises(ValueError, match=msg):
683+
df.groupby(lambda x: x.lower(), as_index=False, axis=1)
672684

673685

674686
def test_groupby_multiple_key(df):
@@ -722,8 +734,11 @@ def test_omit_nuisance(df):
722734

723735
# won't work with axis = 1
724736
grouped = df.groupby({'A': 0, 'C': 0, 'D': 1, 'E': 1}, axis=1)
725-
result = pytest.raises(TypeError, grouped.agg,
726-
lambda x: x.sum(0, numeric_only=False))
737+
msg = (r'\("unsupported operand type\(s\) for \+: '
738+
"'Timestamp' and 'float'\""
739+
r", u?'occurred at index 0'\)")
740+
with pytest.raises(TypeError, match=msg):
741+
grouped.agg(lambda x: x.sum(0, numeric_only=False))
727742

728743

729744
def test_omit_nuisance_python_multiple(three_group):
@@ -756,7 +771,9 @@ def test_empty_groups_corner(mframe):
756771

757772
def test_nonsense_func():
758773
df = DataFrame([0])
759-
pytest.raises(Exception, df.groupby, lambda x: x + 'foo')
774+
msg = r"unsupported operand type\(s\) for \+: '(int|long)' and 'str'"
775+
with pytest.raises(TypeError, match=msg):
776+
df.groupby(lambda x: x + 'foo')
760777

761778

762779
def test_wrap_aggregated_output_multindex(mframe):
@@ -823,12 +840,22 @@ def test_groupby_level_nonmulti():
823840
result = s.groupby(level=[-1]).sum()
824841
tm.assert_series_equal(result, expected)
825842

826-
pytest.raises(ValueError, s.groupby, level=1)
827-
pytest.raises(ValueError, s.groupby, level=-2)
828-
pytest.raises(ValueError, s.groupby, level=[])
829-
pytest.raises(ValueError, s.groupby, level=[0, 0])
830-
pytest.raises(ValueError, s.groupby, level=[0, 1])
831-
pytest.raises(ValueError, s.groupby, level=[1])
843+
msg = "level > 0 or level < -1 only valid with MultiIndex"
844+
with pytest.raises(ValueError, match=msg):
845+
s.groupby(level=1)
846+
with pytest.raises(ValueError, match=msg):
847+
s.groupby(level=-2)
848+
msg = "No group keys passed!"
849+
with pytest.raises(ValueError, match=msg):
850+
s.groupby(level=[])
851+
msg = "multiple levels only valid with MultiIndex"
852+
with pytest.raises(ValueError, match=msg):
853+
s.groupby(level=[0, 0])
854+
with pytest.raises(ValueError, match=msg):
855+
s.groupby(level=[0, 1])
856+
msg = "level > 0 or level < -1 only valid with MultiIndex"
857+
with pytest.raises(ValueError, match=msg):
858+
s.groupby(level=[1])
832859

833860

834861
def test_groupby_complex():
@@ -1101,7 +1128,8 @@ def test_groupby_list_infer_array_like(df):
11011128
expected = df.groupby(df['A']).mean()
11021129
assert_frame_equal(result, expected, check_names=False)
11031130

1104-
pytest.raises(Exception, df.groupby, list(df['A'][:-1]))
1131+
with pytest.raises(KeyError, match=r"^'foo'$"):
1132+
df.groupby(list(df['A'][:-1]))
11051133

11061134
# pathological case of ambiguity
11071135
df = DataFrame({'foo': [0, 1],
@@ -1128,10 +1156,13 @@ def test_groupby_keys_same_size_as_index():
11281156

11291157
def test_groupby_one_row():
11301158
# GH 11741
1159+
msg = r"^'Z'$"
11311160
df1 = pd.DataFrame(np.random.randn(1, 4), columns=list('ABCD'))
1132-
pytest.raises(KeyError, df1.groupby, 'Z')
1161+
with pytest.raises(KeyError, match=msg):
1162+
df1.groupby('Z')
11331163
df2 = pd.DataFrame(np.random.randn(2, 4), columns=list('ABCD'))
1134-
pytest.raises(KeyError, df2.groupby, 'Z')
1164+
with pytest.raises(KeyError, match=msg):
1165+
df2.groupby('Z')
11351166

11361167

11371168
def test_groupby_nat_exclude():
@@ -1169,7 +1200,8 @@ def test_groupby_nat_exclude():
11691200
tm.assert_frame_equal(
11701201
grouped.get_group(Timestamp('2013-02-01')), df.iloc[[3, 5]])
11711202

1172-
pytest.raises(KeyError, grouped.get_group, pd.NaT)
1203+
with pytest.raises(KeyError, match=r"^NaT$"):
1204+
grouped.get_group(pd.NaT)
11731205

11741206
nan_df = DataFrame({'nan': [np.nan, np.nan, np.nan],
11751207
'nat': [pd.NaT, pd.NaT, pd.NaT]})
@@ -1181,8 +1213,10 @@ def test_groupby_nat_exclude():
11811213
assert grouped.groups == {}
11821214
assert grouped.ngroups == 0
11831215
assert grouped.indices == {}
1184-
pytest.raises(KeyError, grouped.get_group, np.nan)
1185-
pytest.raises(KeyError, grouped.get_group, pd.NaT)
1216+
with pytest.raises(KeyError, match=r"^nan$"):
1217+
grouped.get_group(np.nan)
1218+
with pytest.raises(KeyError, match=r"^NaT$"):
1219+
grouped.get_group(pd.NaT)
11861220

11871221

11881222
@pytest.mark.filterwarnings("ignore:\\nPanel:FutureWarning")
@@ -1643,7 +1677,7 @@ def test_pivot_table_values_key_error():
16431677
df['year'] = df.set_index('eventDate').index.year
16441678
df['month'] = df.set_index('eventDate').index.month
16451679

1646-
with pytest.raises(KeyError):
1680+
with pytest.raises(KeyError, match="'badname'"):
16471681
df.reset_index().pivot_table(index='year', columns='month',
16481682
values='badname', aggfunc='count')
16491683

@@ -1689,7 +1723,7 @@ def test_tuple_correct_keyerror():
16891723
df = pd.DataFrame(1, index=range(3),
16901724
columns=pd.MultiIndex.from_product([[1, 2],
16911725
[3, 4]]))
1692-
with pytest.raises(KeyError, match="(7, 8)"):
1726+
with pytest.raises(KeyError, match=r"^\(7, 8\)$"):
16931727
df.groupby((7, 8)).mean()
16941728

16951729

pandas/tests/groupby/test_grouping.py

+34-15
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class TestSelection(object):
2626
def test_select_bad_cols(self):
2727
df = DataFrame([[1, 2]], columns=['A', 'B'])
2828
g = df.groupby('A')
29-
pytest.raises(KeyError, g.__getitem__, ['C']) # g[['C']]
29+
with pytest.raises(KeyError, match='"Columns not found: \'C\'"'):
30+
g[['C']]
3031

31-
pytest.raises(KeyError, g.__getitem__, ['A', 'C']) # g[['A', 'C']]
3232
with pytest.raises(KeyError, match='^[^A]+$'):
3333
# A should not be referenced as a bad column...
3434
# will have to rethink regex if you change message!
@@ -39,8 +39,11 @@ def test_groupby_duplicated_column_errormsg(self):
3939
df = DataFrame(columns=['A', 'B', 'A', 'C'],
4040
data=[range(4), range(2, 6), range(0, 8, 2)])
4141

42-
pytest.raises(ValueError, df.groupby, 'A')
43-
pytest.raises(ValueError, df.groupby, ['A', 'B'])
42+
msg = "Grouper for 'A' not 1-dimensional"
43+
with pytest.raises(ValueError, match=msg):
44+
df.groupby('A')
45+
with pytest.raises(ValueError, match=msg):
46+
df.groupby(['A', 'B'])
4447

4548
grouped = df.groupby('B')
4649
c = grouped.count()
@@ -304,7 +307,8 @@ def test_grouper_iter(self, df):
304307

305308
def test_empty_groups(self, df):
306309
# see gh-1048
307-
pytest.raises(ValueError, df.groupby, [])
310+
with pytest.raises(ValueError, match="No group keys passed!"):
311+
df.groupby([])
308312

309313
def test_groupby_grouper(self, df):
310314
grouped = df.groupby('A')
@@ -345,11 +349,15 @@ def test_groupby_grouper_f_sanity_checked(self):
345349
# when the elements are Timestamp.
346350
# the result is Index[0:6], very confusing.
347351

348-
pytest.raises(AssertionError, ts.groupby, lambda key: key[0:6])
352+
msg = r"Grouper result violates len\(labels\) == len\(data\)"
353+
with pytest.raises(AssertionError, match=msg):
354+
ts.groupby(lambda key: key[0:6])
349355

350356
def test_grouping_error_on_multidim_input(self, df):
351-
pytest.raises(ValueError,
352-
Grouping, df.index, df[['A', 'A']])
357+
msg = ("Grouper for '<class 'pandas.core.frame.DataFrame'>'"
358+
" not 1-dimensional")
359+
with pytest.raises(ValueError, match=msg):
360+
Grouping(df.index, df[['A', 'A']])
353361

354362
def test_multiindex_passthru(self):
355363

@@ -470,14 +478,18 @@ def test_groupby_level(self, sort, mframe, df):
470478
assert_frame_equal(result1, expected1.T)
471479

472480
# raise exception for non-MultiIndex
473-
pytest.raises(ValueError, df.groupby, level=1)
481+
msg = "level > 0 or level < -1 only valid with MultiIndex"
482+
with pytest.raises(ValueError, match=msg):
483+
df.groupby(level=1)
474484

475485
def test_groupby_level_index_names(self):
476486
# GH4014 this used to raise ValueError since 'exp'>1 (in py2)
477487
df = DataFrame({'exp': ['A'] * 3 + ['B'] * 3,
478488
'var1': lrange(6), }).set_index('exp')
479489
df.groupby(level='exp')
480-
pytest.raises(ValueError, df.groupby, level='foo')
490+
msg = "level name foo is not the name of the index"
491+
with pytest.raises(ValueError, match=msg):
492+
df.groupby(level='foo')
481493

482494
@pytest.mark.parametrize('sort', [True, False])
483495
def test_groupby_level_with_nas(self, sort):
@@ -588,10 +600,15 @@ def test_get_group(self):
588600
assert_frame_equal(result1, result3)
589601

590602
# must pass a same-length tuple with multiple keys
591-
pytest.raises(ValueError, lambda: g.get_group('foo'))
592-
pytest.raises(ValueError, lambda: g.get_group(('foo')))
593-
pytest.raises(ValueError,
594-
lambda: g.get_group(('foo', 'bar', 'baz')))
603+
msg = "must supply a tuple to get_group with multiple grouping keys"
604+
with pytest.raises(ValueError, match=msg):
605+
g.get_group('foo')
606+
with pytest.raises(ValueError, match=msg):
607+
g.get_group(('foo'))
608+
msg = ("must supply a same-length tuple to get_group with multiple"
609+
" grouping keys")
610+
with pytest.raises(ValueError, match=msg):
611+
g.get_group(('foo', 'bar', 'baz'))
595612

596613
def test_get_group_empty_bins(self, observed):
597614

@@ -605,7 +622,9 @@ def test_get_group_empty_bins(self, observed):
605622
expected = DataFrame([3, 1], index=[0, 1])
606623
assert_frame_equal(result, expected)
607624

608-
pytest.raises(KeyError, lambda: g.get_group(pd.Interval(10, 15)))
625+
msg = r"Interval\(10, 15, closed='right'\)"
626+
with pytest.raises(KeyError, match=msg):
627+
g.get_group(pd.Interval(10, 15))
609628

610629
def test_get_group_grouped_by_tuple(self):
611630
# GH 8121

0 commit comments

Comments
 (0)