@@ -1072,67 +1072,45 @@ class TestCategoricalSeriesReductions:
10721072 # were moved from a series-specific test file, _not_ that these tests are
10731073 # intended long-term to be series-specific
10741074
1075- def test_min_max (self ):
1075+ @pytest .mark .parametrize ("function" , ["min" , "max" ])
1076+ def test_min_max_unordered_raises (self , function ):
10761077 # unordered cats have no min/max
10771078 cat = Series (Categorical (["a" , "b" , "c" , "d" ], ordered = False ))
1078- with pytest .raises (TypeError ):
1079- cat .min ()
1080- with pytest .raises (TypeError ):
1081- cat .max ()
1082-
1083- cat = Series (Categorical (["a" , "b" , "c" , "d" ], ordered = True ))
1084- _min = cat .min ()
1085- _max = cat .max ()
1086- assert _min == "a"
1087- assert _max == "d"
1088-
1089- cat = Series (
1090- Categorical (
1091- ["a" , "b" , "c" , "d" ], categories = ["d" , "c" , "b" , "a" ], ordered = True
1092- )
1093- )
1094- _min = cat .min ()
1095- _max = cat .max ()
1096- assert _min == "d"
1097- assert _max == "a"
1098-
1099- cat = Series (
1100- Categorical (
1101- [np .nan , "b" , "c" , np .nan ],
1102- categories = ["d" , "c" , "b" , "a" ],
1103- ordered = True ,
1104- )
1105- )
1106- _min = cat .min ()
1107- _max = cat .max ()
1108- assert _min == "c"
1109- assert _max == "b"
1079+ msg = f"Categorical is not ordered for operation { function } "
1080+ with pytest .raises (TypeError , match = msg ):
1081+ getattr (cat , function )()
11101082
1111- cat = Series (
1112- Categorical (
1113- [np .nan , 1 , 2 , np .nan ], categories = [5 , 4 , 3 , 2 , 1 ], ordered = True
1114- )
1115- )
1116- _min = cat .min ()
1117- _max = cat .max ()
1118- assert _min == 2
1119- assert _max == 1
1083+ @pytest .mark .parametrize (
1084+ "values, categories" ,
1085+ [
1086+ (list ("abc" ), list ("abc" )),
1087+ (list ("abc" ), list ("cba" )),
1088+ (list ("abc" ) + [np .nan ], list ("cba" )),
1089+ ([1 , 2 , 3 ], [3 , 2 , 1 ]),
1090+ ([1 , 2 , 3 , np .nan ], [3 , 2 , 1 ]),
1091+ ],
1092+ )
1093+ @pytest .mark .parametrize ("function" , ["min" , "max" ])
1094+ def test_min_max_ordered (self , values , categories , function ):
1095+ # GH 25303
1096+ cat = Series (Categorical (values , categories = categories , ordered = True ))
1097+ result = getattr (cat , function )(skipna = True )
1098+ expected = categories [0 ] if function == "min" else categories [2 ]
1099+ assert result == expected
11201100
1101+ @pytest .mark .parametrize ("function" , ["min" , "max" ])
11211102 @pytest .mark .parametrize ("skipna" , [True , False ])
1122- def test_min_max_skipna (self , skipna ):
1123- # GH 25303
1103+ def test_min_max_skipna (self , function , skipna ):
11241104 cat = Series (
11251105 Categorical (["a" , "b" , np .nan , "a" ], categories = ["b" , "a" ], ordered = True )
11261106 )
1127- _min = cat .min (skipna = skipna )
1128- _max = cat .max (skipna = skipna )
1107+ result = getattr (cat , function )(skipna = skipna )
11291108
11301109 if skipna is True :
1131- assert _min == "b "
1132- assert _max == "a"
1110+ expected = "b" if function == "min" else "a "
1111+ assert result == expected
11331112 else :
1134- assert np .isnan (_min )
1135- assert np .isnan (_max )
1113+ assert result is np .nan
11361114
11371115
11381116class TestSeriesMode :
0 commit comments