Skip to content

Commit d26fbee

Browse files
authored
TST: More pytest.mark.parameterize (#45106)
1 parent 2f915b3 commit d26fbee

File tree

5 files changed

+118
-105
lines changed

5 files changed

+118
-105
lines changed

pandas/tests/arrays/sparse/test_libsparse.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -393,26 +393,27 @@ def test_lookup_array(self):
393393
exp = np.array([-1, -1, 1, -1], dtype=np.int32)
394394
tm.assert_numpy_array_equal(res, exp)
395395

396-
def test_lookup_basics(self):
397-
def _check(index):
398-
assert index.lookup(0) == -1
399-
assert index.lookup(5) == 0
400-
assert index.lookup(7) == 2
401-
assert index.lookup(8) == -1
402-
assert index.lookup(9) == -1
403-
assert index.lookup(10) == -1
404-
assert index.lookup(11) == -1
405-
assert index.lookup(12) == 3
406-
assert index.lookup(17) == 8
407-
assert index.lookup(18) == -1
408-
396+
@pytest.mark.parametrize(
397+
"idx, expected",
398+
[
399+
[0, -1],
400+
[5, 0],
401+
[7, 2],
402+
[8, -1],
403+
[9, -1],
404+
[10, -1],
405+
[11, -1],
406+
[12, 3],
407+
[17, 8],
408+
[18, -1],
409+
],
410+
)
411+
def test_lookup_basics(self, idx, expected):
409412
bindex = BlockIndex(20, [5, 12], [3, 6])
410-
iindex = bindex.to_int_index()
413+
assert bindex.lookup(idx) == expected
411414

412-
_check(bindex)
413-
_check(iindex)
414-
415-
# corner cases
415+
iindex = bindex.to_int_index()
416+
assert iindex.lookup(idx) == expected
416417

417418

418419
class TestBlockIndex:

pandas/tests/frame/test_stack_unstack.py

+46-39
Original file line numberDiff line numberDiff line change
@@ -1421,50 +1421,57 @@ def test_stack(self, multiindex_year_month_day_dataframe_random_data):
14211421
# stack with negative number
14221422
result = ymd.unstack(0).stack(-2)
14231423
expected = ymd.unstack(0).stack(0)
1424+
tm.assert_equal(result, expected)
14241425

1426+
@pytest.mark.parametrize(
1427+
"idx, columns, exp_idx",
1428+
[
1429+
[
1430+
list("abab"),
1431+
["1st", "2nd", "3rd"],
1432+
MultiIndex(
1433+
levels=[["a", "b"], ["1st", "2nd", "3rd"]],
1434+
codes=[
1435+
np.tile(np.arange(2).repeat(3), 2),
1436+
np.tile(np.arange(3), 4),
1437+
],
1438+
),
1439+
],
1440+
[
1441+
list("abab"),
1442+
["1st", "2nd", "1st"],
1443+
MultiIndex(
1444+
levels=[["a", "b"], ["1st", "2nd"]],
1445+
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile([0, 1, 0], 4)],
1446+
),
1447+
],
1448+
[
1449+
MultiIndex.from_tuples((("a", 2), ("b", 1), ("a", 1), ("b", 2))),
1450+
["1st", "2nd", "1st"],
1451+
MultiIndex(
1452+
levels=[["a", "b"], [1, 2], ["1st", "2nd"]],
1453+
codes=[
1454+
np.tile(np.arange(2).repeat(3), 2),
1455+
np.repeat([1, 0, 1], [3, 6, 3]),
1456+
np.tile([0, 1, 0], 4),
1457+
],
1458+
),
1459+
],
1460+
],
1461+
)
1462+
def test_stack_duplicate_index(self, idx, columns, exp_idx):
14251463
# GH10417
1426-
def check(left, right):
1427-
tm.assert_series_equal(left, right)
1428-
assert left.index.is_unique is False
1429-
li, ri = left.index, right.index
1430-
tm.assert_index_equal(li, ri)
1431-
14321464
df = DataFrame(
14331465
np.arange(12).reshape(4, 3),
1434-
index=list("abab"),
1435-
columns=["1st", "2nd", "3rd"],
1466+
index=idx,
1467+
columns=columns,
14361468
)
1437-
1438-
mi = MultiIndex(
1439-
levels=[["a", "b"], ["1st", "2nd", "3rd"]],
1440-
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile(np.arange(3), 4)],
1441-
)
1442-
1443-
left, right = df.stack(), Series(np.arange(12), index=mi)
1444-
check(left, right)
1445-
1446-
df.columns = ["1st", "2nd", "1st"]
1447-
mi = MultiIndex(
1448-
levels=[["a", "b"], ["1st", "2nd"]],
1449-
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile([0, 1, 0], 4)],
1450-
)
1451-
1452-
left, right = df.stack(), Series(np.arange(12), index=mi)
1453-
check(left, right)
1454-
1455-
tpls = ("a", 2), ("b", 1), ("a", 1), ("b", 2)
1456-
df.index = MultiIndex.from_tuples(tpls)
1457-
mi = MultiIndex(
1458-
levels=[["a", "b"], [1, 2], ["1st", "2nd"]],
1459-
codes=[
1460-
np.tile(np.arange(2).repeat(3), 2),
1461-
np.repeat([1, 0, 1], [3, 6, 3]),
1462-
np.tile([0, 1, 0], 4),
1463-
],
1464-
)
1465-
1466-
left, right = df.stack(), Series(np.arange(12), index=mi)
1467-
check(left, right)
1469+
result = df.stack()
1470+
expected = Series(np.arange(12), index=exp_idx)
1471+
tm.assert_series_equal(result, expected)
1472+
assert result.index.is_unique is False
1473+
li, ri = result.index, expected.index
1474+
tm.assert_index_equal(li, ri)
14681475

14691476
def test_unstack_odd_failure(self):
14701477
data = """day,time,smoker,sum,len

pandas/tests/internals/test_internals.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,18 @@ def test_constructor(self):
254254
int32block = create_block("i4", [0])
255255
assert int32block.dtype == np.int32
256256

257-
def test_pickle(self):
258-
def _check(blk):
259-
assert_block_equal(tm.round_trip_pickle(blk), blk)
260-
261-
_check(self.fblock)
262-
_check(self.cblock)
263-
_check(self.oblock)
264-
_check(self.bool_block)
257+
@pytest.mark.parametrize(
258+
"typ, data",
259+
[
260+
["float", [0, 2, 4]],
261+
["complex", [7]],
262+
["object", [1, 3]],
263+
["bool", [5]],
264+
],
265+
)
266+
def test_pickle(self, typ, data):
267+
blk = create_block(typ, data)
268+
assert_block_equal(tm.round_trip_pickle(blk), blk)
265269

266270
def test_mgr_locs(self):
267271
assert isinstance(self.fblock.mgr_locs, BlockPlacement)

pandas/tests/io/formats/test_format.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -175,29 +175,32 @@ def test_eng_float_formatter(self, float_frame):
175175
repr(df)
176176
tm.reset_display_options()
177177

178-
def test_show_null_counts(self):
178+
@pytest.mark.parametrize(
179+
"row, columns, show_counts, result",
180+
[
181+
[20, 20, None, True],
182+
[20, 20, True, True],
183+
[20, 20, False, False],
184+
[5, 5, None, False],
185+
[5, 5, True, False],
186+
[5, 5, False, False],
187+
],
188+
)
189+
def test_show_counts(self, row, columns, show_counts, result):
179190

180191
df = DataFrame(1, columns=range(10), index=range(10))
181192
df.iloc[1, 1] = np.nan
182193

183-
def check(show_counts, result):
184-
buf = StringIO()
185-
df.info(buf=buf, show_counts=show_counts)
186-
assert ("non-null" in buf.getvalue()) is result
187-
188194
with option_context(
189-
"display.max_info_rows", 20, "display.max_info_columns", 20
195+
"display.max_info_rows", row, "display.max_info_columns", columns
190196
):
191-
check(None, True)
192-
check(True, True)
193-
check(False, False)
194-
195-
with option_context("display.max_info_rows", 5, "display.max_info_columns", 5):
196-
check(None, False)
197-
check(True, False)
198-
check(False, False)
197+
with StringIO() as buf:
198+
df.info(buf=buf, show_counts=show_counts)
199+
assert ("non-null" in buf.getvalue()) is result
199200

201+
def test_show_null_counts_deprecation(self):
200202
# GH37999
203+
df = DataFrame(1, columns=range(10), index=range(10))
201204
with tm.assert_produces_warning(
202205
FutureWarning, match="null_counts is deprecated.+"
203206
):

pandas/tests/io/pytables/test_put.py

+23-25
Original file line numberDiff line numberDiff line change
@@ -219,37 +219,35 @@ def test_put_mixed_type(setup_path):
219219
tm.assert_frame_equal(expected, df)
220220

221221

222-
def test_store_index_types(setup_path):
222+
@pytest.mark.parametrize(
223+
"format, index",
224+
[
225+
["table", tm.makeFloatIndex],
226+
["table", tm.makeStringIndex],
227+
["table", tm.makeIntIndex],
228+
["table", tm.makeDateIndex],
229+
["fixed", tm.makeFloatIndex],
230+
["fixed", tm.makeStringIndex],
231+
["fixed", tm.makeIntIndex],
232+
["fixed", tm.makeDateIndex],
233+
["table", tm.makePeriodIndex], # GH#7796
234+
["fixed", tm.makePeriodIndex],
235+
["table", tm.makeUnicodeIndex],
236+
["fixed", tm.makeUnicodeIndex],
237+
],
238+
)
239+
def test_store_index_types(setup_path, format, index):
223240
# GH5386
224241
# test storing various index types
225242

226243
with ensure_clean_store(setup_path) as store:
227244

228-
def check(format, index):
229-
df = DataFrame(np.random.randn(10, 2), columns=list("AB"))
230-
df.index = index(len(df))
231-
232-
_maybe_remove(store, "df")
233-
store.put("df", df, format=format)
234-
tm.assert_frame_equal(df, store["df"])
235-
236-
for index in [
237-
tm.makeFloatIndex,
238-
tm.makeStringIndex,
239-
tm.makeIntIndex,
240-
tm.makeDateIndex,
241-
]:
245+
df = DataFrame(np.random.randn(10, 2), columns=list("AB"))
246+
df.index = index(len(df))
242247

243-
check("table", index)
244-
check("fixed", index)
245-
246-
check("fixed", tm.makePeriodIndex)
247-
check("table", tm.makePeriodIndex) # GH#7796
248-
249-
# unicode
250-
index = tm.makeUnicodeIndex
251-
check("table", index)
252-
check("fixed", index)
248+
_maybe_remove(store, "df")
249+
store.put("df", df, format=format)
250+
tm.assert_frame_equal(df, store["df"])
253251

254252

255253
def test_column_multiindex(setup_path):

0 commit comments

Comments
 (0)