10
10
from pandas .util .testing import assert_frame_equal
11
11
from pandas .tests .frame .common import TestData
12
12
from pandas import Series , DataFrame
13
- from pandas .compat import product
14
13
15
14
16
15
class TestRank (TestData ):
@@ -26,6 +25,13 @@ class TestRank(TestData):
26
25
'dense' : np .array ([1 , 3 , 4 , 2 , nan , 2 , 1 , 5 , nan , 3 ]),
27
26
}
28
27
28
+ @pytest .fixture (params = ['average' , 'min' , 'max' , 'first' , 'dense' ])
29
+ def method (self , request ):
30
+ """
31
+ Fixture for trying all rank methods
32
+ """
33
+ return request .param
34
+
29
35
def test_rank (self ):
30
36
rankdata = pytest .importorskip ('scipy.stats.rankdata' )
31
37
@@ -217,34 +223,35 @@ def test_rank_methods_frame(self):
217
223
expected = expected .astype ('float64' )
218
224
tm .assert_frame_equal (result , expected )
219
225
220
- def test_rank_descending ( self ):
221
- dtypes = [ 'O' , 'f8' , 'i8' ]
226
+ @ pytest . mark . parametrize ( 'dtype' , [ 'O' , 'f8' , 'i8' ])
227
+ def test_rank_descending ( self , method , dtype ):
222
228
223
- for dtype , method in product (dtypes , self .results ):
224
- if 'i' in dtype :
225
- df = self .df .dropna ()
226
- else :
227
- df = self .df .astype (dtype )
229
+ if 'i' in dtype :
230
+ df = self .df .dropna ()
231
+ else :
232
+ df = self .df .astype (dtype )
228
233
229
- res = df .rank (ascending = False )
230
- expected = (df .max () - df ).rank ()
231
- assert_frame_equal (res , expected )
234
+ res = df .rank (ascending = False )
235
+ expected = (df .max () - df ).rank ()
236
+ assert_frame_equal (res , expected )
232
237
233
- if method == 'first' and dtype == 'O' :
234
- continue
238
+ if method == 'first' and dtype == 'O' :
239
+ return
235
240
236
- expected = (df .max () - df ).rank (method = method )
241
+ expected = (df .max () - df ).rank (method = method )
237
242
238
- if dtype != 'O' :
239
- res2 = df .rank (method = method , ascending = False ,
240
- numeric_only = True )
241
- assert_frame_equal (res2 , expected )
243
+ if dtype != 'O' :
244
+ res2 = df .rank (method = method , ascending = False ,
245
+ numeric_only = True )
246
+ assert_frame_equal (res2 , expected )
242
247
243
- res3 = df .rank (method = method , ascending = False ,
244
- numeric_only = False )
245
- assert_frame_equal (res3 , expected )
248
+ res3 = df .rank (method = method , ascending = False ,
249
+ numeric_only = False )
250
+ assert_frame_equal (res3 , expected )
246
251
247
- def test_rank_2d_tie_methods (self ):
252
+ @pytest .mark .parametrize ('axis' , [0 , 1 ])
253
+ @pytest .mark .parametrize ('dtype' , [None , object ])
254
+ def test_rank_2d_tie_methods (self , method , axis , dtype ):
248
255
df = self .df
249
256
250
257
def _check2d (df , expected , method = 'average' , axis = 0 ):
@@ -257,43 +264,38 @@ def _check2d(df, expected, method='average', axis=0):
257
264
result = df .rank (method = method , axis = axis )
258
265
assert_frame_equal (result , exp_df )
259
266
260
- dtypes = [None , object ]
261
267
disabled = set ([(object , 'first' )])
262
- results = self .results
263
-
264
- for method , axis , dtype in product (results , [0 , 1 ], dtypes ):
265
- if (dtype , method ) in disabled :
266
- continue
267
- frame = df if dtype is None else df .astype (dtype )
268
- _check2d (frame , results [method ], method = method , axis = axis )
269
-
270
-
271
- @pytest .mark .parametrize (
272
- "method,exp" , [("dense" ,
273
- [[1. , 1. , 1. ],
274
- [1. , 0.5 , 2. / 3 ],
275
- [1. , 0.5 , 1. / 3 ]]),
276
- ("min" ,
277
- [[1. / 3 , 1. , 1. ],
278
- [1. / 3 , 1. / 3 , 2. / 3 ],
279
- [1. / 3 , 1. / 3 , 1. / 3 ]]),
280
- ("max" ,
281
- [[1. , 1. , 1. ],
282
- [1. , 2. / 3 , 2. / 3 ],
283
- [1. , 2. / 3 , 1. / 3 ]]),
284
- ("average" ,
285
- [[2. / 3 , 1. , 1. ],
286
- [2. / 3 , 0.5 , 2. / 3 ],
287
- [2. / 3 , 0.5 , 1. / 3 ]]),
288
- ("first" ,
289
- [[1. / 3 , 1. , 1. ],
290
- [2. / 3 , 1. / 3 , 2. / 3 ],
291
- [3. / 3 , 2. / 3 , 1. / 3 ]])])
292
- def test_rank_pct_true (method , exp ):
293
- # see gh-15630.
294
-
295
- df = DataFrame ([[2012 , 66 , 3 ], [2012 , 65 , 2 ], [2012 , 65 , 1 ]])
296
- result = df .rank (method = method , pct = True )
297
-
298
- expected = DataFrame (exp )
299
- tm .assert_frame_equal (result , expected )
268
+ if (dtype , method ) in disabled :
269
+ return
270
+ frame = df if dtype is None else df .astype (dtype )
271
+ _check2d (frame , self .results [method ], method = method , axis = axis )
272
+
273
+ @pytest .mark .parametrize (
274
+ "method,exp" , [("dense" ,
275
+ [[1. , 1. , 1. ],
276
+ [1. , 0.5 , 2. / 3 ],
277
+ [1. , 0.5 , 1. / 3 ]]),
278
+ ("min" ,
279
+ [[1. / 3 , 1. , 1. ],
280
+ [1. / 3 , 1. / 3 , 2. / 3 ],
281
+ [1. / 3 , 1. / 3 , 1. / 3 ]]),
282
+ ("max" ,
283
+ [[1. , 1. , 1. ],
284
+ [1. , 2. / 3 , 2. / 3 ],
285
+ [1. , 2. / 3 , 1. / 3 ]]),
286
+ ("average" ,
287
+ [[2. / 3 , 1. , 1. ],
288
+ [2. / 3 , 0.5 , 2. / 3 ],
289
+ [2. / 3 , 0.5 , 1. / 3 ]]),
290
+ ("first" ,
291
+ [[1. / 3 , 1. , 1. ],
292
+ [2. / 3 , 1. / 3 , 2. / 3 ],
293
+ [3. / 3 , 2. / 3 , 1. / 3 ]])])
294
+ def test_rank_pct_true (self , method , exp ):
295
+ # see gh-15630.
296
+
297
+ df = DataFrame ([[2012 , 66 , 3 ], [2012 , 65 , 2 ], [2012 , 65 , 1 ]])
298
+ result = df .rank (method = method , pct = True )
299
+
300
+ expected = DataFrame (exp )
301
+ tm .assert_frame_equal (result , expected )
0 commit comments