55
66from pandas .compat import PYPY
77
8- from pandas import Categorical , Index , Series
8+ from pandas import Categorical , Index , NaT , Series , date_range
99from pandas .api .types import is_scalar
1010import pandas .util .testing as tm
1111
1212
1313class TestCategoricalAnalytics :
14- def test_min_max ( self ):
15-
14+ @ pytest . mark . parametrize ( "aggregation" , [ "min" , "max" ])
15+ def test_min_max_not_ordered_raises ( self , aggregation ):
1616 # unordered cats have no min/max
1717 cat = Categorical (["a" , "b" , "c" , "d" ], ordered = False )
1818 msg = "Categorical is not ordered for operation {}"
19- with pytest . raises ( TypeError , match = msg . format ( "min" )):
20- cat . min ()
21- with pytest .raises (TypeError , match = msg .format ("max" )):
22- cat . max ()
19+ agg_func = getattr ( cat , aggregation )
20+
21+ with pytest .raises (TypeError , match = msg .format (aggregation )):
22+ agg_func ()
2323
24+ def test_min_max_ordered (self ):
2425 cat = Categorical (["a" , "b" , "c" , "d" ], ordered = True )
2526 _min = cat .min ()
2627 _max = cat .max ()
@@ -35,6 +36,29 @@ def test_min_max(self):
3536 assert _min == "d"
3637 assert _max == "a"
3738
39+ @pytest .mark .parametrize (
40+ "categories,expected" ,
41+ [
42+ (list ("ABC" ), np .NaN ),
43+ ([1 , 2 , 3 ], np .NaN ),
44+ pytest .param (
45+ Series (date_range ("2020-01-01" , periods = 3 ), dtype = "category" ),
46+ NaT ,
47+ marks = pytest .mark .xfail (
48+ reason = "https://github.com/pandas-dev/pandas/issues/29962"
49+ ),
50+ ),
51+ ],
52+ )
53+ @pytest .mark .parametrize ("aggregation" , ["min" , "max" ])
54+ def test_min_max_ordered_empty (self , categories , expected , aggregation ):
55+ # GH 30227
56+ cat = Categorical ([], categories = list ("ABC" ), ordered = True )
57+
58+ agg_func = getattr (cat , aggregation )
59+ result = agg_func ()
60+ assert result is expected
61+
3862 @pytest .mark .parametrize ("skipna" , [True , False ])
3963 def test_min_max_with_nan (self , skipna ):
4064 # GH 25303
0 commit comments