Skip to content

[TST] Fixturize frame/test_rank.py #26733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions pandas/tests/frame/test_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def method(self, request):
"""
return request.param

def test_rank(self):
def test_rank(self, float_frame):
rankdata = pytest.importorskip('scipy.stats.rankdata')

self.frame['A'][::2] = np.nan
self.frame['B'][::3] = np.nan
self.frame['C'][::4] = np.nan
self.frame['D'][::5] = np.nan
float_frame['A'][::2] = np.nan
float_frame['B'][::3] = np.nan
float_frame['C'][::4] = np.nan
float_frame['D'][::5] = np.nan

ranks0 = self.frame.rank()
ranks1 = self.frame.rank(1)
mask = np.isnan(self.frame.values)
ranks0 = float_frame.rank()
ranks1 = float_frame.rank(1)
mask = np.isnan(float_frame.values)

fvals = self.frame.fillna(np.inf).values
fvals = float_frame.fillna(np.inf).values

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

# mixed-type frames
self.mixed_frame['datetime'] = datetime.now()
self.mixed_frame['timedelta'] = timedelta(days=1, seconds=1)

result = self.mixed_frame.rank(1)
expected = self.mixed_frame.rank(1, numeric_only=True)
tm.assert_frame_equal(result, expected)

df = DataFrame({"a": [1e-20, -5, 1e-20 + 1e-40, 10,
1e60, 1e80, 1e-30]})
exp = DataFrame({"a": [3.5, 1., 3.5, 5., 6., 7., 2.]})
tm.assert_frame_equal(df.rank(), exp)

def test_rank_na_option(self):
def test_rank_mixed_frame(self, float_string_frame):
float_string_frame['datetime'] = datetime.now()
float_string_frame['timedelta'] = timedelta(days=1, seconds=1)

result = float_string_frame.rank(1)
expected = float_string_frame.rank(1, numeric_only=True)
tm.assert_frame_equal(result, expected)

def test_rank_na_option(self, float_frame):
rankdata = pytest.importorskip('scipy.stats.rankdata')

self.frame['A'][::2] = np.nan
self.frame['B'][::3] = np.nan
self.frame['C'][::4] = np.nan
self.frame['D'][::5] = np.nan
float_frame['A'][::2] = np.nan
float_frame['B'][::3] = np.nan
float_frame['C'][::4] = np.nan
float_frame['D'][::5] = np.nan

# bottom
ranks0 = self.frame.rank(na_option='bottom')
ranks1 = self.frame.rank(1, na_option='bottom')
ranks0 = float_frame.rank(na_option='bottom')
ranks1 = float_frame.rank(1, na_option='bottom')

fvals = self.frame.fillna(np.inf).values
fvals = float_frame.fillna(np.inf).values

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

# top
ranks0 = self.frame.rank(na_option='top')
ranks1 = self.frame.rank(1, na_option='top')
ranks0 = float_frame.rank(na_option='top')
ranks1 = float_frame.rank(1, na_option='top')

fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values
fval1 = self.frame.T
fval0 = float_frame.fillna((float_frame.min() - 1).to_dict()).values
fval1 = float_frame.T
fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T
fval1 = fval1.fillna(np.inf).values

Expand All @@ -160,10 +160,10 @@ def test_rank_na_option(self):
# descending

# bottom
ranks0 = self.frame.rank(na_option='top', ascending=False)
ranks1 = self.frame.rank(1, na_option='top', ascending=False)
ranks0 = float_frame.rank(na_option='top', ascending=False)
ranks1 = float_frame.rank(1, na_option='top', ascending=False)

fvals = self.frame.fillna(np.inf).values
fvals = float_frame.fillna(np.inf).values

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

# top
ranks0 = self.frame.rank(na_option='bottom', ascending=False)
ranks1 = self.frame.rank(1, na_option='bottom', ascending=False)
ranks0 = float_frame.rank(na_option='bottom', ascending=False)
ranks1 = float_frame.rank(1, na_option='bottom', ascending=False)

fval0 = self.frame.fillna((self.frame.min() - 1).to_dict()).values
fval1 = self.frame.T
fval0 = float_frame.fillna((float_frame.min() - 1).to_dict()).values
fval1 = float_frame.T
fval1 = fval1.fillna((fval1.min() - 1).to_dict()).T
fval1 = fval1.fillna(np.inf).values

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

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

# invalid type
with pytest.raises(ValueError, match=msg):
self.frame.rank(na_option=True, ascending=False)
float_frame.rank(na_option=True, ascending=False)

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