@@ -134,22 +134,28 @@ def test_rank_categorical(self):
134
134
assert_series_equal (ordered .rank (), exp )
135
135
assert_series_equal (ordered .rank (ascending = False ), exp_desc )
136
136
137
- # Unordered categoricals should be ranked as objects
137
+ # See #19560
138
+ error_msg = ("pandas.core.algorithms.rank "
139
+ "not supported for unordered "
140
+ "non-numeric data" )
141
+
142
+ # Ranking unordered categorials depreciated per #19560
138
143
unordered = Series (['first' , 'second' , 'third' , 'fourth' ,
139
144
'fifth' , 'sixth' ]).astype (
140
145
CategoricalDtype (categories = ['first' , 'second' , 'third' ,
141
146
'fourth' , 'fifth' , 'sixth' ],
142
147
ordered = False ))
143
- exp_unordered = Series ([2. , 4. , 6. , 3. , 1. , 5. ])
144
- res = unordered .rank ()
145
- assert_series_equal (res , exp_unordered )
148
+
149
+ with tm .assert_raises_regex (ValueError , error_msg ):
150
+ unordered .rank ()
151
+
146
152
147
153
unordered1 = Series (
148
154
[1 , 2 , 3 , 4 , 5 , 6 ],
149
155
).astype (CategoricalDtype ([1 , 2 , 3 , 4 , 5 , 6 ], False ))
150
- exp_unordered1 = Series ([1. , 2. , 3. , 4. , 5. , 6. ])
156
+
157
+ # Won't raise ValueError because entries not objects.
151
158
res1 = unordered1 .rank ()
152
- assert_series_equal (res1 , exp_unordered1 )
153
159
154
160
# Test na_option for rank data
155
161
na_ser = Series (
@@ -213,16 +219,13 @@ def test_rank_signature(self):
213
219
'int64' ,
214
220
marks = pytest .mark .xfail (
215
221
reason = "iNaT is equivalent to minimum value of dtype"
216
- "int64 pending issue #16674" )),
217
- ([NegInfinity (), '1' , 'A' , 'BA' , 'Ba' , 'C' , Infinity ()],
218
- 'object' )
222
+ "int64 pending issue #16674" ))
219
223
])
220
224
def test_rank_inf (self , contents , dtype ):
221
225
dtype_na_map = {
222
226
'float64' : np .nan ,
223
227
'float32' : np .nan ,
224
- 'int64' : iNaT ,
225
- 'object' : None
228
+ 'int64' : iNaT
226
229
}
227
230
# Insert nans at random positions if underlying dtype has missing
228
231
# value. Then adjust the expected order by adding nans accordingly
@@ -249,13 +252,10 @@ def _check(s, expected, method='average'):
249
252
result = s .rank (method = method )
250
253
tm .assert_series_equal (result , Series (expected ))
251
254
252
- dtypes = [None , object ]
253
- disabled = set ([(object , 'first' )])
255
+ dtypes = [None ]
254
256
results = self .results
255
257
256
258
for method , dtype in product (results , dtypes ):
257
- if (dtype , method ) in disabled :
258
- continue
259
259
series = s if dtype is None else s .astype (dtype )
260
260
_check (series , results [method ], method = method )
261
261
@@ -294,7 +294,7 @@ def _check(s, method, na_option, ascending):
294
294
for dtype , na_value , pos_inf , neg_inf in dtypes :
295
295
in_arr = [neg_inf ] * chunk + [na_value ] * chunk + [pos_inf ] * chunk
296
296
iseries = Series (in_arr , dtype = dtype )
297
- if ( dtype , method ) in disabled :
297
+ if dtype == 'object' :
298
298
continue
299
299
_check (iseries , method , na_option , ascending )
300
300
@@ -330,7 +330,7 @@ def test_rank_methods_series(self):
330
330
tm .assert_series_equal (result , expected )
331
331
332
332
def test_rank_dense_method (self ):
333
- dtypes = ['O' , ' f8' , 'i8' ]
333
+ dtypes = ['f8' , 'i8' ]
334
334
in_out = [([1 ], [1 ]),
335
335
([2 ], [1 ]),
336
336
([0 ], [1 ]),
@@ -348,7 +348,7 @@ def test_rank_dense_method(self):
348
348
assert_series_equal (result , expected )
349
349
350
350
def test_rank_descending (self ):
351
- dtypes = ['O' , ' f8' , 'i8' ]
351
+ dtypes = ['f8' , 'i8' ]
352
352
353
353
for dtype , method in product (dtypes , self .results ):
354
354
if 'i' in dtype :
@@ -360,9 +360,6 @@ def test_rank_descending(self):
360
360
expected = (s .max () - s ).rank ()
361
361
assert_series_equal (res , expected )
362
362
363
- if method == 'first' and dtype == 'O' :
364
- continue
365
-
366
363
expected = (s .max () - s ).rank (method = method )
367
364
res2 = s .rank (method = method , ascending = False )
368
365
assert_series_equal (res2 , expected )
@@ -379,9 +376,15 @@ def test_rank_int(self):
379
376
def test_rank_object_bug (self ):
380
377
# GH 13445
381
378
382
- # smoke tests
383
- Series ([np .nan ] * 32 ).astype (object ).rank (ascending = True )
384
- Series ([np .nan ] * 32 ).astype (object ).rank (ascending = False )
379
+ # See #19560
380
+ error_msg = ("pandas.core.algorithms.rank "
381
+ "not supported for unordered "
382
+ "non-numeric data" )
383
+
384
+ with tm .assert_raises_regex (ValueError , error_msg ):
385
+ Series ([np .nan ] * 32 ).astype (object ).rank (ascending = True )
386
+ with tm .assert_raises_regex (ValueError , error_msg ):
387
+ Series ([np .nan ] * 32 ).astype (object ).rank (ascending = False )
385
388
386
389
def test_rank_modify_inplace (self ):
387
390
# GH 18521
@@ -396,7 +399,7 @@ def test_rank_modify_inplace(self):
396
399
397
400
# GH15630, pct should be on 100% basis when method='dense'
398
401
399
- @pytest .mark .parametrize ('dtype' , ['O' , ' f8' , 'i8' ])
402
+ @pytest .mark .parametrize ('dtype' , ['f8' , 'i8' ])
400
403
@pytest .mark .parametrize ('ser, exp' , [
401
404
([1 ], [1. ]),
402
405
([1 , 2 ], [1. / 2 , 2. / 2 ]),
@@ -414,7 +417,7 @@ def test_rank_dense_pct(dtype, ser, exp):
414
417
assert_series_equal (result , expected )
415
418
416
419
417
- @pytest .mark .parametrize ('dtype' , ['O' , ' f8' , 'i8' ])
420
+ @pytest .mark .parametrize ('dtype' , ['f8' , 'i8' ])
418
421
@pytest .mark .parametrize ('ser, exp' , [
419
422
([1 ], [1. ]),
420
423
([1 , 2 ], [1. / 2 , 2. / 2 ]),
@@ -432,7 +435,7 @@ def test_rank_min_pct(dtype, ser, exp):
432
435
assert_series_equal (result , expected )
433
436
434
437
435
- @pytest .mark .parametrize ('dtype' , ['O' , ' f8' , 'i8' ])
438
+ @pytest .mark .parametrize ('dtype' , ['f8' , 'i8' ])
436
439
@pytest .mark .parametrize ('ser, exp' , [
437
440
([1 ], [1. ]),
438
441
([1 , 2 ], [1. / 2 , 2. / 2 ]),
@@ -450,7 +453,7 @@ def test_rank_max_pct(dtype, ser, exp):
450
453
assert_series_equal (result , expected )
451
454
452
455
453
- @pytest .mark .parametrize ('dtype' , ['O' , ' f8' , 'i8' ])
456
+ @pytest .mark .parametrize ('dtype' , ['f8' , 'i8' ])
454
457
@pytest .mark .parametrize ('ser, exp' , [
455
458
([1 ], [1. ]),
456
459
([1 , 2 ], [1. / 2 , 2. / 2 ]),
0 commit comments