44import pytest
55
66from pandas .compat import lrange
7+ from pandas .compat .numpy import _np_version_under1p16
78
89import pandas as pd
910from pandas import Index , MultiIndex , date_range , period_range
1314def test_shift (idx ):
1415
1516 # GH8083 test the base class for shift
16- pytest .raises (NotImplementedError , idx .shift , 1 )
17- pytest .raises (NotImplementedError , idx .shift , 1 , 2 )
17+ msg = "Not supported for type MultiIndex"
18+ with pytest .raises (NotImplementedError , match = msg ):
19+ idx .shift (1 )
20+ with pytest .raises (NotImplementedError , match = msg ):
21+ idx .shift (1 , 2 )
1822
1923
2024def test_groupby (idx ):
@@ -50,25 +54,26 @@ def test_truncate():
5054 result = index .truncate (before = 1 , after = 2 )
5155 assert len (result .levels [0 ]) == 2
5256
53- # after < before
54- pytest .raises (ValueError , index .truncate , 3 , 1 )
57+ msg = "after < before"
58+ with pytest .raises (ValueError , match = msg ):
59+ index .truncate (3 , 1 )
5560
5661
5762def test_where ():
5863 i = MultiIndex .from_tuples ([('A' , 1 ), ('A' , 2 )])
5964
60- with pytest .raises (NotImplementedError ):
65+ msg = r"\.where is not supported for MultiIndex operations"
66+ with pytest .raises (NotImplementedError , match = msg ):
6167 i .where (True )
6268
6369
64- def test_where_array_like ():
70+ @pytest .mark .parametrize ('klass' , [list , tuple , np .array , pd .Series ])
71+ def test_where_array_like (klass ):
6572 i = MultiIndex .from_tuples ([('A' , 1 ), ('A' , 2 )])
66- klasses = [list , tuple , np .array , pd .Series ]
6773 cond = [False , True ]
68-
69- for klass in klasses :
70- with pytest .raises (NotImplementedError ):
71- i .where (klass (cond ))
74+ msg = r"\.where is not supported for MultiIndex operations"
75+ with pytest .raises (NotImplementedError , match = msg ):
76+ i .where (klass (cond ))
7277
7378
7479# TODO: reshape
@@ -141,7 +146,8 @@ def test_take(idx):
141146 # if not isinstance(idx,
142147 # (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
143148 # GH 10791
144- with pytest .raises (AttributeError ):
149+ msg = "'MultiIndex' object has no attribute 'freq'"
150+ with pytest .raises (AttributeError , match = msg ):
145151 idx .freq
146152
147153
@@ -199,7 +205,8 @@ def test_take_fill_value():
199205 with pytest .raises (ValueError , match = msg ):
200206 idx .take (np .array ([1 , 0 , - 5 ]), fill_value = True )
201207
202- with pytest .raises (IndexError ):
208+ msg = "index -5 is out of bounds for size 4"
209+ with pytest .raises (IndexError , match = msg ):
203210 idx .take (np .array ([1 , - 5 ]))
204211
205212
@@ -215,13 +222,15 @@ def test_sub(idx):
215222 first = idx
216223
217224 # - now raises (previously was set op difference)
218- with pytest .raises (TypeError ):
225+ msg = "cannot perform __sub__ with this index type: MultiIndex"
226+ with pytest .raises (TypeError , match = msg ):
219227 first - idx [- 3 :]
220- with pytest .raises (TypeError ):
228+ with pytest .raises (TypeError , match = msg ):
221229 idx [- 3 :] - first
222- with pytest .raises (TypeError ):
230+ with pytest .raises (TypeError , match = msg ):
223231 idx [- 3 :] - first .tolist ()
224- with pytest .raises (TypeError ):
232+ msg = "cannot perform __rsub__ with this index type: MultiIndex"
233+ with pytest .raises (TypeError , match = msg ):
225234 first .tolist () - idx [- 3 :]
226235
227236
@@ -272,50 +281,28 @@ def test_map_dictlike(idx, mapper):
272281 np .arccos , np .arctan , np .sinh , np .cosh , np .tanh ,
273282 np .arcsinh , np .arccosh , np .arctanh , np .deg2rad ,
274283 np .rad2deg
275- ])
276- def test_numpy_ufuncs (func ):
284+ ], ids = lambda func : func . __name__ )
285+ def test_numpy_ufuncs (idx , func ):
277286 # test ufuncs of numpy. see:
278287 # http://docs.scipy.org/doc/numpy/reference/ufuncs.html
279288
280- # copy and paste from idx fixture as pytest doesn't support
281- # parameters and fixtures at the same time.
282- major_axis = Index (['foo' , 'bar' , 'baz' , 'qux' ])
283- minor_axis = Index (['one' , 'two' ])
284- major_codes = np .array ([0 , 0 , 1 , 2 , 3 , 3 ])
285- minor_codes = np .array ([0 , 1 , 0 , 1 , 0 , 1 ])
286- index_names = ['first' , 'second' ]
287-
288- idx = MultiIndex (
289- levels = [major_axis , minor_axis ],
290- codes = [major_codes , minor_codes ],
291- names = index_names ,
292- verify_integrity = False
293- )
294-
295- with pytest .raises (Exception ):
296- with np .errstate (all = 'ignore' ):
297- func (idx )
289+ if _np_version_under1p16 :
290+ expected_exception = AttributeError
291+ msg = "'tuple' object has no attribute '{}'" .format (func .__name__ )
292+ else :
293+ expected_exception = TypeError
294+ msg = ("loop of ufunc does not support argument 0 of type tuple which"
295+ " has no callable {} method" ).format (func .__name__ )
296+ with pytest .raises (expected_exception , match = msg ):
297+ func (idx )
298298
299299
300300@pytest .mark .parametrize ('func' , [
301301 np .isfinite , np .isinf , np .isnan , np .signbit
302- ])
303- def test_numpy_type_funcs (func ):
304- # for func in [np.isfinite, np.isinf, np.isnan, np.signbit]:
305- # copy and paste from idx fixture as pytest doesn't support
306- # parameters and fixtures at the same time.
307- major_axis = Index (['foo' , 'bar' , 'baz' , 'qux' ])
308- minor_axis = Index (['one' , 'two' ])
309- major_codes = np .array ([0 , 0 , 1 , 2 , 3 , 3 ])
310- minor_codes = np .array ([0 , 1 , 0 , 1 , 0 , 1 ])
311- index_names = ['first' , 'second' ]
312-
313- idx = MultiIndex (
314- levels = [major_axis , minor_axis ],
315- codes = [major_codes , minor_codes ],
316- names = index_names ,
317- verify_integrity = False
318- )
319-
320- with pytest .raises (Exception ):
302+ ], ids = lambda func : func .__name__ )
303+ def test_numpy_type_funcs (idx , func ):
304+ msg = ("ufunc '{}' not supported for the input types, and the inputs"
305+ " could not be safely coerced to any supported types according to"
306+ " the casting rule ''safe''" ).format (func .__name__ )
307+ with pytest .raises (TypeError , match = msg ):
321308 func (idx )
0 commit comments