Skip to content

Commit

Permalink
STY: use pytest.raises context syntax (indexing) (pandas-dev#24960)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and Pingviinituutti committed Feb 28, 2019
1 parent dda1d0f commit 23cf92c
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 154 deletions.
6 changes: 4 additions & 2 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ def test_loc_multiindex(self):
tm.assert_frame_equal(rs, xp)

# missing label
pytest.raises(KeyError, lambda: mi_int.loc[2])
with pytest.raises(KeyError, match=r"^2L?$"):
mi_int.loc[2]
with catch_warnings(record=True):
# GH 21593
pytest.raises(KeyError, lambda: mi_int.ix[2])
with pytest.raises(KeyError, match=r"^2L?$"):
mi_int.ix[2]

def test_loc_multiindex_indexer_none(self):

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def test_getitem_partial_column_select(self):
result = df.ix[('a', 'y'), [1, 0]]
tm.assert_frame_equal(result, expected)

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

def test_partial_set(
self, multiindex_year_month_day_dataframe_random_data):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexing/multiindex/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def test_per_axis_per_level_getitem(self):
# ambiguous cases
# these can be multiply interpreted (e.g. in this case
# as df.loc[slice(None),[1]] as well
pytest.raises(KeyError, lambda: df.loc[slice(None), [1]])
with pytest.raises(KeyError, match=r"'\[1\] not in index'"):
df.loc[slice(None), [1]]

result = df.loc[(slice(None), [1]), :]
expected = df.iloc[[0, 3]]
Expand Down
52 changes: 30 additions & 22 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,20 @@ def test_loc_scalar(self):
assert_frame_equal(df, expected)

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

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

pytest.raises(TypeError, f)

def f():
msg = ("cannot insert an item into a CategoricalIndex that is not"
" already an existing category")
with pytest.raises(TypeError, match=msg):
df.loc['d', 'A'] = 10

pytest.raises(TypeError, f)

def f():
with pytest.raises(TypeError, match=msg):
df.loc['d', 'C'] = 10

pytest.raises(TypeError, f)

def test_getitem_scalar(self):

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

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

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

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

# args NotImplemented ATM
pytest.raises(NotImplementedError,
lambda: self.df2.reindex(['a'], method='ffill'))
pytest.raises(NotImplementedError,
lambda: self.df2.reindex(['a'], level=1))
pytest.raises(NotImplementedError,
lambda: self.df2.reindex(['a'], limit=2))
msg = r"argument {} is not implemented for CategoricalIndex\.reindex"
with pytest.raises(NotImplementedError, match=msg.format('method')):
self.df2.reindex(['a'], method='ffill')
with pytest.raises(NotImplementedError, match=msg.format('level')):
self.df2.reindex(['a'], level=1)
with pytest.raises(NotImplementedError, match=msg.format('limit')):
self.df2.reindex(['a'], limit=2)

def test_loc_slice(self):
# slicing
# not implemented ATM
# GH9748

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

# result = df.loc[1:5]
# expected = df.iloc[[1,2,3,4]]
Expand Down Expand Up @@ -679,8 +684,11 @@ def test_boolean_selection(self):
# categories=[3, 2, 1],
# ordered=False,
# name=u'B')
pytest.raises(TypeError, lambda: df4[df4.index < 2])
pytest.raises(TypeError, lambda: df4[df4.index > 1])
msg = "Unordered Categoricals can only compare equality or not"
with pytest.raises(TypeError, match=msg):
df4[df4.index < 2]
with pytest.raises(TypeError, match=msg):
df4[df4.index > 1]

def test_indexing_with_category(self):

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,11 @@ def test_setting_with_copy_bug(self):
'c': ['a', 'b', np.nan, 'd']})
mask = pd.isna(df.c)

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

pytest.raises(com.SettingWithCopyError, f)

# invalid warning as we are returning a new object
# GH 8730
df1 = DataFrame({'x': Series(['a', 'b', 'c']),
Expand Down
Loading

0 comments on commit 23cf92c

Please sign in to comment.