@@ -1277,12 +1277,11 @@ def test_mode(self):
1277
1277
exp = Categorical ([4 ], categories = [5 , 4 , 3 , 2 , 1 ], ordered = True )
1278
1278
self .assertTrue (res .equals (exp ))
1279
1279
1280
- def test_sort (self ):
1280
+ def test_sort_values (self ):
1281
1281
1282
1282
# unordered cats are sortable
1283
1283
cat = Categorical (["a" , "b" , "b" , "a" ], ordered = False )
1284
1284
cat .sort_values ()
1285
- cat .sort ()
1286
1285
1287
1286
cat = Categorical (["a" , "c" , "b" , "d" ], ordered = True )
1288
1287
@@ -1303,10 +1302,62 @@ def test_sort(self):
1303
1302
1304
1303
# sort (inplace order)
1305
1304
cat1 = cat .copy ()
1306
- cat1 .sort ( )
1305
+ cat1 .sort_values ( inplace = True )
1307
1306
exp = np .array (["a" , "b" , "c" , "d" ], dtype = object )
1308
1307
self .assert_numpy_array_equal (cat1 .__array__ (), exp )
1309
1308
1309
+ # reverse
1310
+ cat = Categorical (["a" , "c" , "c" , "b" , "d" ], ordered = True )
1311
+ res = cat .sort_values (ascending = False )
1312
+ exp_val = np .array (["d" , "c" , "c" , "b" , "a" ], dtype = object )
1313
+ exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
1314
+ self .assert_numpy_array_equal (res .__array__ (), exp_val )
1315
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1316
+
1317
+ def test_sort_values_na_position (self ):
1318
+ # see gh-12882
1319
+ cat = Categorical ([5 , 2 , np .nan , 2 , np .nan ], ordered = True )
1320
+ exp_categories = np .array ([2 , 5 ])
1321
+
1322
+ exp = np .array ([2.0 , 2.0 , 5.0 , np .nan , np .nan ])
1323
+ res = cat .sort_values () # default arguments
1324
+ self .assert_numpy_array_equal (res .__array__ (), exp )
1325
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1326
+
1327
+ exp = np .array ([np .nan , np .nan , 2.0 , 2.0 , 5.0 ])
1328
+ res = cat .sort_values (ascending = True , na_position = 'first' )
1329
+ self .assert_numpy_array_equal (res .__array__ (), exp )
1330
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1331
+
1332
+ exp = np .array ([np .nan , np .nan , 5.0 , 2.0 , 2.0 ])
1333
+ res = cat .sort_values (ascending = False , na_position = 'first' )
1334
+ self .assert_numpy_array_equal (res .__array__ (), exp )
1335
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1336
+
1337
+ exp = np .array ([2.0 , 2.0 , 5.0 , np .nan , np .nan ])
1338
+ res = cat .sort_values (ascending = True , na_position = 'last' )
1339
+ self .assert_numpy_array_equal (res .__array__ (), exp )
1340
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1341
+
1342
+ exp = np .array ([5.0 , 2.0 , 2.0 , np .nan , np .nan ])
1343
+ res = cat .sort_values (ascending = False , na_position = 'last' )
1344
+ self .assert_numpy_array_equal (res .__array__ (), exp )
1345
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1346
+
1347
+ cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
1348
+ res = cat .sort_values (ascending = False , na_position = 'last' )
1349
+ exp_val = np .array (["d" , "c" , "b" , "a" , np .nan ], dtype = object )
1350
+ exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
1351
+ self .assert_numpy_array_equal (res .__array__ (), exp_val )
1352
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1353
+
1354
+ cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
1355
+ res = cat .sort_values (ascending = False , na_position = 'first' )
1356
+ exp_val = np .array ([np .nan , "d" , "c" , "b" , "a" ], dtype = object )
1357
+ exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
1358
+ self .assert_numpy_array_equal (res .__array__ (), exp_val )
1359
+ self .assert_numpy_array_equal (res .categories , exp_categories )
1360
+
1310
1361
def test_slicing_directly (self ):
1311
1362
cat = Categorical (["a" , "b" , "c" , "d" , "a" , "b" , "c" ])
1312
1363
sliced = cat [3 ]
@@ -2951,14 +3002,16 @@ def test_count(self):
2951
3002
result = s .count ()
2952
3003
self .assertEqual (result , 2 )
2953
3004
2954
- def test_sort (self ):
3005
+ def test_sort_values (self ):
2955
3006
2956
3007
c = Categorical (["a" , "b" , "b" , "a" ], ordered = False )
2957
- cat = Series (c )
3008
+ cat = Series (c . copy () )
2958
3009
2959
- # 9816 deprecated
2960
- with tm .assert_produces_warning (FutureWarning ):
2961
- c .order ()
3010
+ # 'order' was deprecated in gh-10726
3011
+ # 'sort' was deprecated in gh-12882
3012
+ for func in ('order' , 'sort' ):
3013
+ with tm .assert_produces_warning (FutureWarning ):
3014
+ getattr (c , func )()
2962
3015
2963
3016
# sort in the categories order
2964
3017
expected = Series (
@@ -3024,44 +3077,6 @@ def test_sort(self):
3024
3077
expected = df .iloc [[2 , 1 , 5 , 4 , 3 , 0 ]]
3025
3078
tm .assert_frame_equal (result , expected )
3026
3079
3027
- # reverse
3028
- cat = Categorical (["a" , "c" , "c" , "b" , "d" ], ordered = True )
3029
- res = cat .sort_values (ascending = False )
3030
- exp_val = np .array (["d" , "c" , "c" , "b" , "a" ], dtype = object )
3031
- exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
3032
- self .assert_numpy_array_equal (res .__array__ (), exp_val )
3033
- self .assert_numpy_array_equal (res .categories , exp_categories )
3034
-
3035
- # some NaN positions
3036
-
3037
- cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
3038
- res = cat .sort_values (ascending = False , na_position = 'last' )
3039
- exp_val = np .array (["d" , "c" , "b" , "a" , np .nan ], dtype = object )
3040
- exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
3041
- self .assert_numpy_array_equal (res .__array__ (), exp_val )
3042
- self .assert_numpy_array_equal (res .categories , exp_categories )
3043
-
3044
- cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
3045
- res = cat .sort_values (ascending = False , na_position = 'first' )
3046
- exp_val = np .array ([np .nan , "d" , "c" , "b" , "a" ], dtype = object )
3047
- exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
3048
- self .assert_numpy_array_equal (res .__array__ (), exp_val )
3049
- self .assert_numpy_array_equal (res .categories , exp_categories )
3050
-
3051
- cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
3052
- res = cat .sort_values (ascending = False , na_position = 'first' )
3053
- exp_val = np .array ([np .nan , "d" , "c" , "b" , "a" ], dtype = object )
3054
- exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
3055
- self .assert_numpy_array_equal (res .__array__ (), exp_val )
3056
- self .assert_numpy_array_equal (res .categories , exp_categories )
3057
-
3058
- cat = Categorical (["a" , "c" , "b" , "d" , np .nan ], ordered = True )
3059
- res = cat .sort_values (ascending = False , na_position = 'last' )
3060
- exp_val = np .array (["d" , "c" , "b" , "a" , np .nan ], dtype = object )
3061
- exp_categories = np .array (["a" , "b" , "c" , "d" ], dtype = object )
3062
- self .assert_numpy_array_equal (res .__array__ (), exp_val )
3063
- self .assert_numpy_array_equal (res .categories , exp_categories )
3064
-
3065
3080
def test_slicing (self ):
3066
3081
cat = Series (Categorical ([1 , 2 , 3 , 4 ]))
3067
3082
reversed = cat [::- 1 ]
0 commit comments