Skip to content

Commit a3aed2f

Browse files
committed
Rename tests to nsorted and rename order to columns
1 parent 5b0767a commit a3aed2f

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

pandas/tests/frame/methods/test_nlargest.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
Note: for naming purposes, most tests are title with as e.g. "test_nlargest_foo"
3-
but are implicitly also testing nsmallest_foo.
2+
Note: for naming purposes, most test method titles include "nsorted"
3+
(e.g., "test_nlargest_foo") but are implicitly also testing "nsmallest" and
4+
"nlargest".
45
"""
56

67
from string import ascii_lowercase
@@ -41,11 +42,11 @@ def df_main_dtypes():
4142
)
4243

4344

44-
class TestNLargestNSmallest:
45+
class TestNSorted:
4546
# ----------------------------------------------------------------------
4647
# Top / bottom
4748
@pytest.mark.parametrize(
48-
"order",
49+
"columns",
4950
[
5051
["a"],
5152
["c"],
@@ -63,7 +64,7 @@ class TestNLargestNSmallest:
6364
],
6465
)
6566
@pytest.mark.parametrize("n", range(1, 11))
66-
def test_nlargest_n(self, nselect_method, n, order):
67+
def test_nsorted_n(self, nselect_method, n: int, columns):
6768
# GH#10393
6869
df = pd.DataFrame(
6970
{
@@ -72,24 +73,24 @@ def test_nlargest_n(self, nselect_method, n, order):
7273
"c": np.random.default_rng(2).permutation(10).astype("float64"),
7374
}
7475
)
75-
if "b" in order:
76+
if "b" in columns:
7677
error_msg = (
7778
f"Column 'b' has dtype (object|str), "
7879
f"cannot use method '{nselect_method}' with this dtype"
7980
)
8081
with pytest.raises(TypeError, match=error_msg):
81-
getattr(df, nselect_method)(n, order)
82+
getattr(df, nselect_method)(n, columns)
8283
else:
8384
ascending = nselect_method == "nsmallest"
84-
result = getattr(df, nselect_method)(n, order)
85+
result = getattr(df, nselect_method)(n, columns)
8586
result.index = pd.Index(list(result.index))
86-
expected = df.sort_values(order, ascending=ascending).head(n)
87+
expected = df.sort_values(columns, ascending=ascending).head(n)
8788
tm.assert_frame_equal(result, expected)
8889

8990
@pytest.mark.parametrize(
9091
"columns", [["group", "category_string"], ["group", "string"]]
9192
)
92-
def test_nlargest_error(self, df_main_dtypes, nselect_method, columns):
93+
def test_nsorted_error(self, df_main_dtypes, nselect_method, columns):
9394
df = df_main_dtypes
9495
col = columns[1]
9596
error_msg = (
@@ -106,12 +107,12 @@ def test_nlargest_error(self, df_main_dtypes, nselect_method, columns):
106107
with pytest.raises(TypeError, match=error_msg):
107108
getattr(df, nselect_method)(2, columns)
108109

109-
def test_nlargest_all_dtypes(self, df_main_dtypes):
110+
def test_nsorted_all_dtypes(self, df_main_dtypes):
110111
df = df_main_dtypes
111112
df.nsmallest(2, list(set(df) - {"category_string", "string"}))
112113
df.nlargest(2, list(set(df) - {"category_string", "string"}))
113114

114-
def test_nlargest_duplicates_on_starter_columns(self):
115+
def test_nsorted_duplicates_on_starter_columns(self):
115116
# regression test for GH#22752
116117

117118
df = pd.DataFrame({"a": [2, 2, 2, 1, 1, 1], "b": [1, 2, 3, 3, 2, 1]})
@@ -128,7 +129,7 @@ def test_nlargest_duplicates_on_starter_columns(self):
128129
)
129130
tm.assert_frame_equal(result, expected)
130131

131-
def test_nlargest_n_identical_values(self):
132+
def test_nsorted_n_identical_values(self):
132133
# GH#15297
133134
df = pd.DataFrame({"a": [1] * 5, "b": [1, 2, 3, 4, 5]})
134135

@@ -141,25 +142,26 @@ def test_nlargest_n_identical_values(self):
141142
tm.assert_frame_equal(result, expected)
142143

143144
@pytest.mark.parametrize(
144-
"order",
145+
"columns",
145146
[["a", "b", "c"], ["c", "b", "a"], ["a"], ["b"], ["a", "b"], ["c", "b"]],
146147
)
147148
@pytest.mark.parametrize("n", range(1, 6))
148-
def test_nlargest_n_duplicate_index(self, n, order, request):
149+
def test_nsorted_n_duplicate_index(self, n: int, columns, request):
149150
# GH#13412
150151

151152
df = pd.DataFrame(
152153
{"a": [1, 2, 3, 4, 4], "b": [1, 1, 1, 1, 1], "c": [0, 1, 2, 5, 4]},
153154
index=[0, 0, 1, 1, 1],
154155
)
155-
result = df.nsmallest(n, order)
156-
expected = df.sort_values(order, kind="stable").head(n)
156+
result = df.nsmallest(n, columns)
157+
expected = df.sort_values(columns, kind="stable").head(n)
157158
tm.assert_frame_equal(result, expected)
158159

159-
result = df.nlargest(n, order)
160-
expected = df.sort_values(order, ascending=False, kind="stable").head(n)
160+
result = df.nlargest(n, columns)
161+
expected = df.sort_values(columns, ascending=False, kind="stable").head(n)
161162
if Version(np.__version__) >= Version("1.25") and (
162-
(order == ["a"] and n in (1, 2, 3, 4)) or ((order == ["a", "b"]) and n == 5)
163+
(columns == ["a"] and n in (1, 2, 3, 4))
164+
or ((columns == ["a", "b"]) and n == 5)
163165
):
164166
request.applymarker(
165167
pytest.mark.xfail(
@@ -172,7 +174,7 @@ def test_nlargest_n_duplicate_index(self, n, order, request):
172174
)
173175
tm.assert_frame_equal(result, expected)
174176

175-
def test_nlargest_duplicate_keep_all_ties(self):
177+
def test_nsorted_duplicate_keep_all_ties(self):
176178
# GH#16818
177179
df = pd.DataFrame(
178180
{"a": [5, 4, 4, 2, 3, 3, 3, 3], "b": [10, 9, 8, 7, 5, 50, 10, 20]}
@@ -197,7 +199,7 @@ def test_nlargest_duplicate_keep_all_ties(self):
197199
)
198200
tm.assert_frame_equal(result, expected)
199201

200-
def test_nlargest_multiindex_column_lookup(self):
202+
def test_nsorted_multiindex_column_lookup(self):
201203
# Check whether tuples are correctly treated as multi-level lookups.
202204
# GH#23033
203205
df = pd.DataFrame(

0 commit comments

Comments
 (0)