@@ -114,6 +114,9 @@ def f():
114
114
Categorical ([1 ,2 ], [1 ,2 ,np .nan , np .nan ])
115
115
self .assertRaises (ValueError , f )
116
116
117
+ # The default should be unordered
118
+ c1 = Categorical (["a" , "b" , "c" , "a" ])
119
+ self .assertFalse (c1 .ordered )
117
120
118
121
# Categorical as input
119
122
c1 = Categorical (["a" , "b" , "c" , "a" ])
@@ -367,6 +370,13 @@ def f():
367
370
self .assertRaises (TypeError , lambda : a < cat )
368
371
self .assertRaises (TypeError , lambda : a < cat_rev )
369
372
373
+ # Make sure that unequal comparison take the categories order in account
374
+ cat_rev = pd .Categorical (list ("abc" ), categories = list ("cba" ), ordered = True )
375
+ exp = np .array ([True , False , False ])
376
+ res = cat_rev > "b"
377
+ self .assert_numpy_array_equal (res , exp )
378
+
379
+
370
380
def test_na_flags_int_categories (self ):
371
381
# #1457
372
382
@@ -2390,6 +2400,14 @@ def test_comparisons(self):
2390
2400
exp = Series ([False , False , True ])
2391
2401
tm .assert_series_equal (res , exp )
2392
2402
2403
+ scalar = base [1 ]
2404
+ res = cat > scalar
2405
+ exp = Series ([False , False , True ])
2406
+ tm .assert_series_equal (res , exp )
2407
+ res_rev = cat_rev > scalar
2408
+ exp_rev = Series ([True , False , False ])
2409
+ tm .assert_series_equal (res_rev , exp_rev )
2410
+
2393
2411
# Only categories with same categories can be compared
2394
2412
def f ():
2395
2413
cat > cat_rev
@@ -2412,6 +2430,17 @@ def f():
2412
2430
res = cat_rev > base [0 ]
2413
2431
tm .assert_series_equal (res , exp )
2414
2432
2433
+ # unequal comparison should raise for unordered cats
2434
+ cat = Series (Categorical (list ("abc" )))
2435
+ def f ():
2436
+ cat > "b"
2437
+ self .assertRaises (TypeError , f )
2438
+ cat = Series (Categorical (list ("abc" ), ordered = False ))
2439
+ def f ():
2440
+ cat > "b"
2441
+ self .assertRaises (TypeError , f )
2442
+
2443
+
2415
2444
# And test NaN handling...
2416
2445
cat = Series (Categorical (["a" ,"b" ,"c" , np .nan ]))
2417
2446
exp = Series ([True , True , True , False ])
0 commit comments