4
4
import numpy as np
5
5
import pytest
6
6
7
+ from pandas .compat import PY2
8
+
7
9
import pandas as pd
8
10
from pandas import Categorical , DataFrame , Series , date_range
9
11
from pandas .tests .arrays .categorical .common import TestCategorical
@@ -17,6 +19,7 @@ def test_categories_none_comparisons(self):
17
19
'a' , 'c' , 'c' , 'c' ], ordered = True )
18
20
tm .assert_categorical_equal (factor , self .factor )
19
21
22
+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
20
23
def test_comparisons (self ):
21
24
22
25
result = self .factor [self .factor == 'a' ]
@@ -95,16 +98,24 @@ def test_comparisons(self):
95
98
96
99
# comparison (in both directions) with Series will raise
97
100
s = Series (["b" , "b" , "b" ])
98
- pytest .raises (TypeError , lambda : cat > s )
99
- pytest .raises (TypeError , lambda : cat_rev > s )
100
- pytest .raises (TypeError , lambda : s < cat )
101
- pytest .raises (TypeError , lambda : s < cat_rev )
101
+ msg = ("Cannot compare a Categorical for op __gt__ with type"
102
+ r" <class 'numpy\.ndarray'>" )
103
+ with pytest .raises (TypeError , match = msg ):
104
+ cat > s
105
+ with pytest .raises (TypeError , match = msg ):
106
+ cat_rev > s
107
+ with pytest .raises (TypeError , match = msg ):
108
+ s < cat
109
+ with pytest .raises (TypeError , match = msg ):
110
+ s < cat_rev
102
111
103
112
# comparison with numpy.array will raise in both direction, but only on
104
113
# newer numpy versions
105
114
a = np .array (["b" , "b" , "b" ])
106
- pytest .raises (TypeError , lambda : cat > a )
107
- pytest .raises (TypeError , lambda : cat_rev > a )
115
+ with pytest .raises (TypeError , match = msg ):
116
+ cat > a
117
+ with pytest .raises (TypeError , match = msg ):
118
+ cat_rev > a
108
119
109
120
# Make sure that unequal comparison take the categories order in
110
121
# account
@@ -163,16 +174,23 @@ def test_comparison_with_unknown_scalars(self):
163
174
# for unequal comps, but not for equal/not equal
164
175
cat = Categorical ([1 , 2 , 3 ], ordered = True )
165
176
166
- pytest .raises (TypeError , lambda : cat < 4 )
167
- pytest .raises (TypeError , lambda : cat > 4 )
168
- pytest .raises (TypeError , lambda : 4 < cat )
169
- pytest .raises (TypeError , lambda : 4 > cat )
177
+ msg = ("Cannot compare a Categorical for op __{}__ with a scalar,"
178
+ " which is not a category" )
179
+ with pytest .raises (TypeError , match = msg .format ('lt' )):
180
+ cat < 4
181
+ with pytest .raises (TypeError , match = msg .format ('gt' )):
182
+ cat > 4
183
+ with pytest .raises (TypeError , match = msg .format ('gt' )):
184
+ 4 < cat
185
+ with pytest .raises (TypeError , match = msg .format ('lt' )):
186
+ 4 > cat
170
187
171
188
tm .assert_numpy_array_equal (cat == 4 ,
172
189
np .array ([False , False , False ]))
173
190
tm .assert_numpy_array_equal (cat != 4 ,
174
191
np .array ([True , True , True ]))
175
192
193
+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
176
194
@pytest .mark .parametrize ('data,reverse,base' , [
177
195
(list ("abc" ), list ("cba" ), list ("bbb" )),
178
196
([1 , 2 , 3 ], [3 , 2 , 1 ], [2 , 2 , 2 ])]
@@ -219,16 +237,26 @@ def test_comparisons(self, data, reverse, base):
219
237
220
238
# categorical cannot be compared to Series or numpy array, and also
221
239
# not the other way around
222
- pytest .raises (TypeError , lambda : cat > s )
223
- pytest .raises (TypeError , lambda : cat_rev > s )
224
- pytest .raises (TypeError , lambda : cat > a )
225
- pytest .raises (TypeError , lambda : cat_rev > a )
240
+ msg = ("Cannot compare a Categorical for op __gt__ with type"
241
+ r" <class 'numpy\.ndarray'>" )
242
+ with pytest .raises (TypeError , match = msg ):
243
+ cat > s
244
+ with pytest .raises (TypeError , match = msg ):
245
+ cat_rev > s
246
+ with pytest .raises (TypeError , match = msg ):
247
+ cat > a
248
+ with pytest .raises (TypeError , match = msg ):
249
+ cat_rev > a
226
250
227
- pytest .raises (TypeError , lambda : s < cat )
228
- pytest .raises (TypeError , lambda : s < cat_rev )
251
+ with pytest .raises (TypeError , match = msg ):
252
+ s < cat
253
+ with pytest .raises (TypeError , match = msg ):
254
+ s < cat_rev
229
255
230
- pytest .raises (TypeError , lambda : a < cat )
231
- pytest .raises (TypeError , lambda : a < cat_rev )
256
+ with pytest .raises (TypeError , match = msg ):
257
+ a < cat
258
+ with pytest .raises (TypeError , match = msg ):
259
+ a < cat_rev
232
260
233
261
@pytest .mark .parametrize ('ctor' , [
234
262
lambda * args , ** kwargs : Categorical (* args , ** kwargs ),
@@ -287,16 +315,21 @@ def test_numeric_like_ops(self):
287
315
right = False , labels = cat_labels )
288
316
289
317
# numeric ops should not succeed
290
- for op in ['__add__' , '__sub__' , '__mul__' , '__truediv__' ]:
291
- pytest .raises (TypeError ,
292
- lambda : getattr (df , op )(df ))
318
+ for op , str_rep in [('__add__' , r'\+' ),
319
+ ('__sub__' , '-' ),
320
+ ('__mul__' , r'\*' ),
321
+ ('__truediv__' , '/' )]:
322
+ msg = r"Series cannot perform the operation {}" .format (str_rep )
323
+ with pytest .raises (TypeError , match = msg ):
324
+ getattr (df , op )(df )
293
325
294
326
# reduction ops should not succeed (unless specifically defined, e.g.
295
327
# min/max)
296
328
s = df ['value_group' ]
297
329
for op in ['kurt' , 'skew' , 'var' , 'std' , 'mean' , 'sum' , 'median' ]:
298
- pytest .raises (TypeError ,
299
- lambda : getattr (s , op )(numeric_only = False ))
330
+ msg = "Categorical cannot perform the operation {}" .format (op )
331
+ with pytest .raises (TypeError , match = msg ):
332
+ getattr (s , op )(numeric_only = False )
300
333
301
334
# mad technically works because it takes always the numeric data
302
335
@@ -306,8 +339,13 @@ def test_numeric_like_ops(self):
306
339
np .sum (s )
307
340
308
341
# numeric ops on a Series
309
- for op in ['__add__' , '__sub__' , '__mul__' , '__truediv__' ]:
310
- pytest .raises (TypeError , lambda : getattr (s , op )(2 ))
342
+ for op , str_rep in [('__add__' , r'\+' ),
343
+ ('__sub__' , '-' ),
344
+ ('__mul__' , r'\*' ),
345
+ ('__truediv__' , '/' )]:
346
+ msg = r"Series cannot perform the operation {}" .format (str_rep )
347
+ with pytest .raises (TypeError , match = msg ):
348
+ getattr (s , op )(2 )
311
349
312
350
# invalid ufunc
313
351
with pytest .raises (TypeError ):
0 commit comments