4
4
import pytest
5
5
6
6
from pandas .compat import lrange
7
+ from pandas .compat .numpy import _np_version_under1p16
7
8
8
9
import pandas as pd
9
10
from pandas import Index , MultiIndex , date_range , period_range
13
14
def test_shift (idx ):
14
15
15
16
# 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 )
18
22
19
23
20
24
def test_groupby (idx ):
@@ -50,25 +54,26 @@ def test_truncate():
50
54
result = index .truncate (before = 1 , after = 2 )
51
55
assert len (result .levels [0 ]) == 2
52
56
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 )
55
60
56
61
57
62
def test_where ():
58
63
i = MultiIndex .from_tuples ([('A' , 1 ), ('A' , 2 )])
59
64
60
- with pytest .raises (NotImplementedError ):
65
+ msg = r"\.where is not supported for MultiIndex operations"
66
+ with pytest .raises (NotImplementedError , match = msg ):
61
67
i .where (True )
62
68
63
69
64
- def test_where_array_like ():
70
+ @pytest .mark .parametrize ('klass' , [list , tuple , np .array , pd .Series ])
71
+ def test_where_array_like (klass ):
65
72
i = MultiIndex .from_tuples ([('A' , 1 ), ('A' , 2 )])
66
- klasses = [list , tuple , np .array , pd .Series ]
67
73
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 ))
72
77
73
78
74
79
# TODO: reshape
@@ -141,7 +146,8 @@ def test_take(idx):
141
146
# if not isinstance(idx,
142
147
# (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
143
148
# GH 10791
144
- with pytest .raises (AttributeError ):
149
+ msg = "'MultiIndex' object has no attribute 'freq'"
150
+ with pytest .raises (AttributeError , match = msg ):
145
151
idx .freq
146
152
147
153
@@ -199,7 +205,8 @@ def test_take_fill_value():
199
205
with pytest .raises (ValueError , match = msg ):
200
206
idx .take (np .array ([1 , 0 , - 5 ]), fill_value = True )
201
207
202
- with pytest .raises (IndexError ):
208
+ msg = "index -5 is out of bounds for size 4"
209
+ with pytest .raises (IndexError , match = msg ):
203
210
idx .take (np .array ([1 , - 5 ]))
204
211
205
212
@@ -215,13 +222,15 @@ def test_sub(idx):
215
222
first = idx
216
223
217
224
# - 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 ):
219
227
first - idx [- 3 :]
220
- with pytest .raises (TypeError ):
228
+ with pytest .raises (TypeError , match = msg ):
221
229
idx [- 3 :] - first
222
- with pytest .raises (TypeError ):
230
+ with pytest .raises (TypeError , match = msg ):
223
231
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 ):
225
234
first .tolist () - idx [- 3 :]
226
235
227
236
@@ -272,50 +281,28 @@ def test_map_dictlike(idx, mapper):
272
281
np .arccos , np .arctan , np .sinh , np .cosh , np .tanh ,
273
282
np .arcsinh , np .arccosh , np .arctanh , np .deg2rad ,
274
283
np .rad2deg
275
- ])
276
- def test_numpy_ufuncs (func ):
284
+ ], ids = lambda func : func . __name__ )
285
+ def test_numpy_ufuncs (idx , func ):
277
286
# test ufuncs of numpy. see:
278
287
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
279
288
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 )
298
298
299
299
300
300
@pytest .mark .parametrize ('func' , [
301
301
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 ):
321
308
func (idx )
0 commit comments