Skip to content

Commit c6fdb6b

Browse files
makbigcjreback
authored andcommitted
Fixturize frame/test_rank.py (#26733)
1 parent cf25c5c commit c6fdb6b

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

pandas/tests/frame/test_rank.py

+38-38
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ def method(self, request):
2929
"""
3030
return request.param
3131

32-
def test_rank(self):
32+
def test_rank(self, float_frame):
3333
rankdata = pytest.importorskip('scipy.stats.rankdata')
3434

35-
self.frame['A'][::2] = np.nan
36-
self.frame['B'][::3] = np.nan
37-
self.frame['C'][::4] = np.nan
38-
self.frame['D'][::5] = np.nan
35+
float_frame['A'][::2] = np.nan
36+
float_frame['B'][::3] = np.nan
37+
float_frame['C'][::4] = np.nan
38+
float_frame['D'][::5] = np.nan
3939

40-
ranks0 = self.frame.rank()
41-
ranks1 = self.frame.rank(1)
42-
mask = np.isnan(self.frame.values)
40+
ranks0 = float_frame.rank()
41+
ranks1 = float_frame.rank(1)
42+
mask = np.isnan(float_frame.values)
4343

44-
fvals = self.frame.fillna(np.inf).values
44+
fvals = float_frame.fillna(np.inf).values
4545

4646
exp0 = np.apply_along_axis(rankdata, 0, fvals)
4747
exp0[mask] = np.nan
@@ -109,32 +109,32 @@ def test_rank2(self):
109109
result = df.rank(1, numeric_only=False, ascending=False)
110110
tm.assert_frame_equal(result, expected)
111111

112-
# mixed-type frames
113-
self.mixed_frame['datetime'] = datetime.now()
114-
self.mixed_frame['timedelta'] = timedelta(days=1, seconds=1)
115-
116-
result = self.mixed_frame.rank(1)
117-
expected = self.mixed_frame.rank(1, numeric_only=True)
118-
tm.assert_frame_equal(result, expected)
119-
120112
df = DataFrame({"a": [1e-20, -5, 1e-20 + 1e-40, 10,
121113
1e60, 1e80, 1e-30]})
122114
exp = DataFrame({"a": [3.5, 1., 3.5, 5., 6., 7., 2.]})
123115
tm.assert_frame_equal(df.rank(), exp)
124116

125-
def test_rank_na_option(self):
117+
def test_rank_mixed_frame(self, float_string_frame):
118+
float_string_frame['datetime'] = datetime.now()
119+
float_string_frame['timedelta'] = timedelta(days=1, seconds=1)
120+
121+
result = float_string_frame.rank(1)
122+
expected = float_string_frame.rank(1, numeric_only=True)
123+
tm.assert_frame_equal(result, expected)
124+
125+
def test_rank_na_option(self, float_frame):
126126
rankdata = pytest.importorskip('scipy.stats.rankdata')
127127

128-
self.frame['A'][::2] = np.nan
129-
self.frame['B'][::3] = np.nan
130-
self.frame['C'][::4] = np.nan
131-
self.frame['D'][::5] = np.nan
128+
float_frame['A'][::2] = np.nan
129+
float_frame['B'][::3] = np.nan
130+
float_frame['C'][::4] = np.nan
131+
float_frame['D'][::5] = np.nan
132132

133133
# bottom
134-
ranks0 = self.frame.rank(na_option='bottom')
135-
ranks1 = self.frame.rank(1, na_option='bottom')
134+
ranks0 = float_frame.rank(na_option='bottom')
135+
ranks1 = float_frame.rank(1, na_option='bottom')
136136

137-
fvals = self.frame.fillna(np.inf).values
137+
fvals = float_frame.fillna(np.inf).values
138138

139139
exp0 = np.apply_along_axis(rankdata, 0, fvals)
140140
exp1 = np.apply_along_axis(rankdata, 1, fvals)
@@ -143,11 +143,11 @@ def test_rank_na_option(self):
143143
tm.assert_almost_equal(ranks1.values, exp1)
144144

145145
# top
146-
ranks0 = self.frame.rank(na_option='top')
147-
ranks1 = self.frame.rank(1, na_option='top')
146+
ranks0 = float_frame.rank(na_option='top')
147+
ranks1 = float_frame.rank(1, na_option='top')
148148

149-
fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values
150-
fval1 = self.frame.T
149+
fval0 = float_frame.fillna((float_frame.min() - 1).to_dict()).values
150+
fval1 = float_frame.T
151151
fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T
152152
fval1 = fval1.fillna(np.inf).values
153153

@@ -160,10 +160,10 @@ def test_rank_na_option(self):
160160
# descending
161161

162162
# bottom
163-
ranks0 = self.frame.rank(na_option='top', ascending=False)
164-
ranks1 = self.frame.rank(1, na_option='top', ascending=False)
163+
ranks0 = float_frame.rank(na_option='top', ascending=False)
164+
ranks1 = float_frame.rank(1, na_option='top', ascending=False)
165165

166-
fvals = self.frame.fillna(np.inf).values
166+
fvals = float_frame.fillna(np.inf).values
167167

168168
exp0 = np.apply_along_axis(rankdata, 0, -fvals)
169169
exp1 = np.apply_along_axis(rankdata, 1, -fvals)
@@ -174,11 +174,11 @@ def test_rank_na_option(self):
174174
# descending
175175

176176
# top
177-
ranks0 = self.frame.rank(na_option='bottom', ascending=False)
178-
ranks1 = self.frame.rank(1, na_option='bottom', ascending=False)
177+
ranks0 = float_frame.rank(na_option='bottom', ascending=False)
178+
ranks1 = float_frame.rank(1, na_option='bottom', ascending=False)
179179

180-
fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values
181-
fval1 = self.frame.T
180+
fval0 = float_frame.fillna((float_frame.min() - 1).to_dict()).values
181+
fval1 = float_frame.T
182182
fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T
183183
fval1 = fval1.fillna(np.inf).values
184184

@@ -192,11 +192,11 @@ def test_rank_na_option(self):
192192
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
193193

194194
with pytest.raises(ValueError, match=msg):
195-
self.frame.rank(na_option='bad', ascending=False)
195+
float_frame.rank(na_option='bad', ascending=False)
196196

197197
# invalid type
198198
with pytest.raises(ValueError, match=msg):
199-
self.frame.rank(na_option=True, ascending=False)
199+
float_frame.rank(na_option=True, ascending=False)
200200

201201
def test_rank_axis(self):
202202
# check if using axes' names gives the same result

0 commit comments

Comments
 (0)