Skip to content

Commit 0132f80

Browse files
simonjayhawkinsPingviinituutti
authored andcommitted
STY: use pytest.raises context syntax (indexing) (pandas-dev#24960)
1 parent 1a2d325 commit 0132f80

11 files changed

+307
-154
lines changed

pandas/tests/indexing/multiindex/test_loc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ def test_loc_multiindex(self):
123123
tm.assert_frame_equal(rs, xp)
124124

125125
# missing label
126-
pytest.raises(KeyError, lambda: mi_int.loc[2])
126+
with pytest.raises(KeyError, match=r"^2L?$"):
127+
mi_int.loc[2]
127128
with catch_warnings(record=True):
128129
# GH 21593
129-
pytest.raises(KeyError, lambda: mi_int.ix[2])
130+
with pytest.raises(KeyError, match=r"^2L?$"):
131+
mi_int.ix[2]
130132

131133
def test_loc_multiindex_indexer_none(self):
132134

pandas/tests/indexing/multiindex/test_partial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def test_getitem_partial_column_select(self):
104104
result = df.ix[('a', 'y'), [1, 0]]
105105
tm.assert_frame_equal(result, expected)
106106

107-
pytest.raises(KeyError, df.loc.__getitem__,
108-
(('a', 'foo'), slice(None, None)))
107+
with pytest.raises(KeyError, match=r"\('a', 'foo'\)"):
108+
df.loc[('a', 'foo'), :]
109109

110110
def test_partial_set(
111111
self, multiindex_year_month_day_dataframe_random_data):

pandas/tests/indexing/multiindex/test_slice.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def test_per_axis_per_level_getitem(self):
107107
# ambiguous cases
108108
# these can be multiply interpreted (e.g. in this case
109109
# as df.loc[slice(None),[1]] as well
110-
pytest.raises(KeyError, lambda: df.loc[slice(None), [1]])
110+
with pytest.raises(KeyError, match=r"'\[1\] not in index'"):
111+
df.loc[slice(None), [1]]
111112

112113
result = df.loc[(slice(None), [1]), :]
113114
expected = df.iloc[[0, 3]]

pandas/tests/indexing/test_categorical.py

+30-22
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,20 @@ def test_loc_scalar(self):
5353
assert_frame_equal(df, expected)
5454

5555
# value not in the categories
56-
pytest.raises(KeyError, lambda: df.loc['d'])
56+
with pytest.raises(KeyError, match=r"^'d'$"):
57+
df.loc['d']
5758

58-
def f():
59+
msg = "cannot append a non-category item to a CategoricalIndex"
60+
with pytest.raises(TypeError, match=msg):
5961
df.loc['d'] = 10
6062

61-
pytest.raises(TypeError, f)
62-
63-
def f():
63+
msg = ("cannot insert an item into a CategoricalIndex that is not"
64+
" already an existing category")
65+
with pytest.raises(TypeError, match=msg):
6466
df.loc['d', 'A'] = 10
65-
66-
pytest.raises(TypeError, f)
67-
68-
def f():
67+
with pytest.raises(TypeError, match=msg):
6968
df.loc['d', 'C'] = 10
7069

71-
pytest.raises(TypeError, f)
72-
7370
def test_getitem_scalar(self):
7471

7572
cats = Categorical([Timestamp('12-31-1999'),
@@ -318,7 +315,8 @@ def test_loc_listlike(self):
318315
assert_frame_equal(result, expected, check_index_type=True)
319316

320317
# element in the categories but not in the values
321-
pytest.raises(KeyError, lambda: self.df2.loc['e'])
318+
with pytest.raises(KeyError, match=r"^'e'$"):
319+
self.df2.loc['e']
322320

323321
# assign is ok
324322
df = self.df2.copy()
@@ -616,22 +614,29 @@ def test_reindexing(self):
616614
assert_frame_equal(result, expected, check_index_type=True)
617615

618616
# passed duplicate indexers are not allowed
619-
pytest.raises(ValueError, lambda: self.df2.reindex(['a', 'a']))
617+
msg = "cannot reindex with a non-unique indexer"
618+
with pytest.raises(ValueError, match=msg):
619+
self.df2.reindex(['a', 'a'])
620620

621621
# args NotImplemented ATM
622-
pytest.raises(NotImplementedError,
623-
lambda: self.df2.reindex(['a'], method='ffill'))
624-
pytest.raises(NotImplementedError,
625-
lambda: self.df2.reindex(['a'], level=1))
626-
pytest.raises(NotImplementedError,
627-
lambda: self.df2.reindex(['a'], limit=2))
622+
msg = r"argument {} is not implemented for CategoricalIndex\.reindex"
623+
with pytest.raises(NotImplementedError, match=msg.format('method')):
624+
self.df2.reindex(['a'], method='ffill')
625+
with pytest.raises(NotImplementedError, match=msg.format('level')):
626+
self.df2.reindex(['a'], level=1)
627+
with pytest.raises(NotImplementedError, match=msg.format('limit')):
628+
self.df2.reindex(['a'], limit=2)
628629

629630
def test_loc_slice(self):
630631
# slicing
631632
# not implemented ATM
632633
# GH9748
633634

634-
pytest.raises(TypeError, lambda: self.df.loc[1:5])
635+
msg = ("cannot do slice indexing on {klass} with these "
636+
r"indexers \[1\] of {kind}".format(
637+
klass=str(CategoricalIndex), kind=str(int)))
638+
with pytest.raises(TypeError, match=msg):
639+
self.df.loc[1:5]
635640

636641
# result = df.loc[1:5]
637642
# expected = df.iloc[[1,2,3,4]]
@@ -679,8 +684,11 @@ def test_boolean_selection(self):
679684
# categories=[3, 2, 1],
680685
# ordered=False,
681686
# name=u'B')
682-
pytest.raises(TypeError, lambda: df4[df4.index < 2])
683-
pytest.raises(TypeError, lambda: df4[df4.index > 1])
687+
msg = "Unordered Categoricals can only compare equality or not"
688+
with pytest.raises(TypeError, match=msg):
689+
df4[df4.index < 2]
690+
with pytest.raises(TypeError, match=msg):
691+
df4[df4.index > 1]
684692

685693
def test_indexing_with_category(self):
686694

pandas/tests/indexing/test_chaining_and_caching.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,11 @@ def test_setting_with_copy_bug(self):
302302
'c': ['a', 'b', np.nan, 'd']})
303303
mask = pd.isna(df.c)
304304

305-
def f():
305+
msg = ("A value is trying to be set on a copy of a slice from a"
306+
" DataFrame")
307+
with pytest.raises(com.SettingWithCopyError, match=msg):
306308
df[['c']][mask] = df[['b']][mask]
307309

308-
pytest.raises(com.SettingWithCopyError, f)
309-
310310
# invalid warning as we are returning a new object
311311
# GH 8730
312312
df1 = DataFrame({'x': Series(['a', 'b', 'c']),

0 commit comments

Comments
 (0)