5
5
from numpy import nan
6
6
import numpy as np
7
7
8
- from pandas import Series , DataFrame
8
+ from pandas import Series
9
9
10
10
from pandas .compat import product
11
- from pandas .util .testing import ( assert_frame_equal , assert_series_equal )
11
+ from pandas .util .testing import assert_series_equal
12
12
import pandas .util .testing as tm
13
13
14
14
15
- class TestRank (tm .TestCase ):
15
+ class TestSeriesRank (tm .TestCase ):
16
16
s = Series ([1 , 3 , 4 , 2 , nan , 2 , 1 , 5 , nan , 3 ])
17
- df = DataFrame ({'A' : s , 'B' : s })
18
17
19
18
results = {
20
19
'average' : np .array ([1.5 , 5.5 , 7.0 , 3.5 , nan ,
@@ -42,6 +41,7 @@ def _check(s, expected, method='average'):
42
41
series = s if dtype is None else s .astype (dtype )
43
42
_check (series , results [method ], method = method )
44
43
44
+
45
45
def test_rank_methods_series (self ):
46
46
tm .skip_if_no_package ('scipy' , '0.13' , 'scipy.stats.rankdata' )
47
47
import scipy
@@ -65,31 +65,6 @@ def test_rank_methods_series(self):
65
65
expected = expected .astype ('float64' )
66
66
tm .assert_series_equal (result , expected )
67
67
68
- def test_rank_methods_frame (self ):
69
- tm .skip_if_no_package ('scipy' , '0.13' , 'scipy.stats.rankdata' )
70
- import scipy
71
- from scipy .stats import rankdata
72
-
73
- xs = np .random .randint (0 , 21 , (100 , 26 ))
74
- xs = (xs - 10.0 ) / 10.0
75
- cols = [chr (ord ('z' ) - i ) for i in range (xs .shape [1 ])]
76
-
77
- for vals in [xs , xs + 1e6 , xs * 1e-6 ]:
78
- df = DataFrame (vals , columns = cols )
79
-
80
- for ax in [0 , 1 ]:
81
- for m in ['average' , 'min' , 'max' , 'first' , 'dense' ]:
82
- result = df .rank (axis = ax , method = m )
83
- sprank = np .apply_along_axis (
84
- rankdata , ax , vals ,
85
- m if m != 'first' else 'ordinal' )
86
- sprank = sprank .astype (np .float64 )
87
- expected = DataFrame (sprank , columns = cols )
88
-
89
- if LooseVersion (scipy .__version__ ) >= '0.17.0' :
90
- expected = expected .astype ('float64' )
91
- tm .assert_frame_equal (result , expected )
92
-
93
68
def test_rank_dense_method (self ):
94
69
dtypes = ['O' , 'f8' , 'i8' ]
95
70
in_out = [([1 ], [1 ]),
@@ -106,67 +81,28 @@ def test_rank_dense_method(self):
106
81
s = Series (ser ).astype (dtype )
107
82
result = s .rank (method = 'dense' )
108
83
expected = Series (exp ).astype (result .dtype )
109
- assert_series_equal (result , expected )
84
+ assert_series_equal (result , expected )
110
85
111
86
def test_rank_descending (self ):
112
87
dtypes = ['O' , 'f8' , 'i8' ]
113
88
114
89
for dtype , method in product (dtypes , self .results ):
115
90
if 'i' in dtype :
116
91
s = self .s .dropna ()
117
- df = self .df .dropna ()
118
92
else :
119
93
s = self .s .astype (dtype )
120
- df = self .df .astype (dtype )
121
94
122
95
res = s .rank (ascending = False )
123
96
expected = (s .max () - s ).rank ()
124
97
assert_series_equal (res , expected )
125
98
126
- res = df .rank (ascending = False )
127
- expected = (df .max () - df ).rank ()
128
- assert_frame_equal (res , expected )
129
-
130
99
if method == 'first' and dtype == 'O' :
131
100
continue
132
101
133
102
expected = (s .max () - s ).rank (method = method )
134
103
res2 = s .rank (method = method , ascending = False )
135
104
assert_series_equal (res2 , expected )
136
105
137
- expected = (df .max () - df ).rank (method = method )
138
-
139
- if dtype != 'O' :
140
- res2 = df .rank (method = method , ascending = False ,
141
- numeric_only = True )
142
- assert_frame_equal (res2 , expected )
143
-
144
- res3 = df .rank (method = method , ascending = False ,
145
- numeric_only = False )
146
- assert_frame_equal (res3 , expected )
147
-
148
- def test_rank_2d_tie_methods (self ):
149
- df = self .df
150
-
151
- def _check2d (df , expected , method = 'average' , axis = 0 ):
152
- exp_df = DataFrame ({'A' : expected , 'B' : expected })
153
-
154
- if axis == 1 :
155
- df = df .T
156
- exp_df = exp_df .T
157
-
158
- result = df .rank (method = method , axis = axis )
159
- assert_frame_equal (result , exp_df )
160
-
161
- dtypes = [None , object ]
162
- disabled = set ([(object , 'first' )])
163
- results = self .results
164
-
165
- for method , axis , dtype in product (results , [0 , 1 ], dtypes ):
166
- if (dtype , method ) in disabled :
167
- continue
168
- frame = df if dtype is None else df .astype (dtype )
169
- _check2d (frame , results [method ], method = method , axis = axis )
170
106
171
107
def test_rank_int (self ):
172
108
s = self .s .dropna ().astype ('i8' )
0 commit comments