44import numpy as np
55import pytest
66
7+ from pandas .compat import PY2
8+
79import pandas as pd
810from pandas import Categorical , DataFrame , Series , date_range
911from pandas .tests .arrays .categorical .common import TestCategorical
@@ -17,6 +19,7 @@ def test_categories_none_comparisons(self):
1719 'a' , 'c' , 'c' , 'c' ], ordered = True )
1820 tm .assert_categorical_equal (factor , self .factor )
1921
22+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
2023 def test_comparisons (self ):
2124
2225 result = self .factor [self .factor == 'a' ]
@@ -95,16 +98,24 @@ def test_comparisons(self):
9598
9699 # comparison (in both directions) with Series will raise
97100 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
102111
103112 # comparison with numpy.array will raise in both direction, but only on
104113 # newer numpy versions
105114 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
108119
109120 # Make sure that unequal comparison take the categories order in
110121 # account
@@ -163,16 +174,23 @@ def test_comparison_with_unknown_scalars(self):
163174 # for unequal comps, but not for equal/not equal
164175 cat = Categorical ([1 , 2 , 3 ], ordered = True )
165176
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
170187
171188 tm .assert_numpy_array_equal (cat == 4 ,
172189 np .array ([False , False , False ]))
173190 tm .assert_numpy_array_equal (cat != 4 ,
174191 np .array ([True , True , True ]))
175192
193+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
176194 @pytest .mark .parametrize ('data,reverse,base' , [
177195 (list ("abc" ), list ("cba" ), list ("bbb" )),
178196 ([1 , 2 , 3 ], [3 , 2 , 1 ], [2 , 2 , 2 ])]
@@ -219,16 +237,26 @@ def test_comparisons(self, data, reverse, base):
219237
220238 # categorical cannot be compared to Series or numpy array, and also
221239 # 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
226250
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
229255
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
232260
233261 @pytest .mark .parametrize ('ctor' , [
234262 lambda * args , ** kwargs : Categorical (* args , ** kwargs ),
@@ -287,16 +315,21 @@ def test_numeric_like_ops(self):
287315 right = False , labels = cat_labels )
288316
289317 # 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 )
293325
294326 # reduction ops should not succeed (unless specifically defined, e.g.
295327 # min/max)
296328 s = df ['value_group' ]
297329 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 )
300333
301334 # mad technically works because it takes always the numeric data
302335
@@ -306,8 +339,13 @@ def test_numeric_like_ops(self):
306339 np .sum (s )
307340
308341 # 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 )
311349
312350 # invalid ufunc
313351 with pytest .raises (TypeError ):
0 commit comments