Skip to content

Commit de82ced

Browse files
authored
TST: More pytest.mark.parameterize (#45115)
1 parent ae7916c commit de82ced

File tree

5 files changed

+135
-162
lines changed

5 files changed

+135
-162
lines changed

pandas/tests/frame/test_constructors.py

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -775,16 +775,6 @@ def test_constructor_dict_of_generators(self):
775775
tm.assert_frame_equal(result, expected)
776776

777777
def test_constructor_dict_multiindex(self):
778-
def check(result, expected):
779-
return tm.assert_frame_equal(
780-
result,
781-
expected,
782-
check_dtype=True,
783-
check_index_type=True,
784-
check_column_type=True,
785-
check_names=True,
786-
)
787-
788778
d = {
789779
("a", "a"): {("i", "i"): 0, ("i", "j"): 1, ("j", "i"): 2},
790780
("b", "a"): {("i", "i"): 6, ("i", "j"): 5, ("j", "i"): 4},
@@ -796,7 +786,10 @@ def check(result, expected):
796786
[x[1] for x in _d], index=MultiIndex.from_tuples([x[0] for x in _d])
797787
).T
798788
expected.index = MultiIndex.from_tuples(expected.index)
799-
check(df, expected)
789+
tm.assert_frame_equal(
790+
df,
791+
expected,
792+
)
800793

801794
d["z"] = {"y": 123.0, ("i", "i"): 111, ("i", "j"): 111, ("j", "i"): 111}
802795
_d.insert(0, ("z", d["z"]))
@@ -806,7 +799,7 @@ def check(result, expected):
806799
expected.index = Index(expected.index, tupleize_cols=False)
807800
df = DataFrame(d)
808801
df = df.reindex(columns=expected.columns, index=expected.index)
809-
check(df, expected)
802+
tm.assert_frame_equal(df, expected)
810803

811804
def test_constructor_dict_datetime64_index(self):
812805
# GH 10160
@@ -2167,44 +2160,38 @@ def test_constructor_series_copy(self, float_frame):
21672160

21682161
assert not (series["A"] == 5).all()
21692162

2170-
def test_constructor_with_nas(self):
2163+
@pytest.mark.parametrize(
2164+
"df",
2165+
[
2166+
DataFrame([[1, 2, 3], [4, 5, 6]], index=[1, np.nan]),
2167+
DataFrame([[1, 2, 3], [4, 5, 6]], columns=[1.1, 2.2, np.nan]),
2168+
DataFrame([[0, 1, 2, 3], [4, 5, 6, 7]], columns=[np.nan, 1.1, 2.2, np.nan]),
2169+
DataFrame(
2170+
[[0.0, 1, 2, 3.0], [4, 5, 6, 7]], columns=[np.nan, 1.1, 2.2, np.nan]
2171+
),
2172+
DataFrame([[0.0, 1, 2, 3.0], [4, 5, 6, 7]], columns=[np.nan, 1, 2, 2]),
2173+
],
2174+
)
2175+
def test_constructor_with_nas(self, df):
21712176
# GH 5016
21722177
# na's in indices
2178+
# GH 21428 (non-unique columns)
21732179

2174-
def check(df):
2175-
for i in range(len(df.columns)):
2176-
df.iloc[:, i]
2177-
2178-
indexer = np.arange(len(df.columns))[isna(df.columns)]
2179-
2180-
# No NaN found -> error
2181-
if len(indexer) == 0:
2182-
with pytest.raises(KeyError, match="^nan$"):
2183-
df.loc[:, np.nan]
2184-
# single nan should result in Series
2185-
elif len(indexer) == 1:
2186-
tm.assert_series_equal(df.iloc[:, indexer[0]], df.loc[:, np.nan])
2187-
# multiple nans should result in DataFrame
2188-
else:
2189-
tm.assert_frame_equal(df.iloc[:, indexer], df.loc[:, np.nan])
2190-
2191-
df = DataFrame([[1, 2, 3], [4, 5, 6]], index=[1, np.nan])
2192-
check(df)
2193-
2194-
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=[1.1, 2.2, np.nan])
2195-
check(df)
2196-
2197-
df = DataFrame([[0, 1, 2, 3], [4, 5, 6, 7]], columns=[np.nan, 1.1, 2.2, np.nan])
2198-
check(df)
2180+
for i in range(len(df.columns)):
2181+
df.iloc[:, i]
21992182

2200-
df = DataFrame(
2201-
[[0.0, 1, 2, 3.0], [4, 5, 6, 7]], columns=[np.nan, 1.1, 2.2, np.nan]
2202-
)
2203-
check(df)
2183+
indexer = np.arange(len(df.columns))[isna(df.columns)]
22042184

2205-
# GH 21428 (non-unique columns)
2206-
df = DataFrame([[0.0, 1, 2, 3.0], [4, 5, 6, 7]], columns=[np.nan, 1, 2, 2])
2207-
check(df)
2185+
# No NaN found -> error
2186+
if len(indexer) == 0:
2187+
with pytest.raises(KeyError, match="^nan$"):
2188+
df.loc[:, np.nan]
2189+
# single nan should result in Series
2190+
elif len(indexer) == 1:
2191+
tm.assert_series_equal(df.iloc[:, indexer[0]], df.loc[:, np.nan])
2192+
# multiple nans should result in DataFrame
2193+
else:
2194+
tm.assert_frame_equal(df.iloc[:, indexer], df.loc[:, np.nan])
22082195

22092196
def test_constructor_lists_to_object_dtype(self):
22102197
# from #1074

pandas/tests/groupby/test_libgroupby.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,35 +130,32 @@ class TestGroupVarFloat32(GroupVarTestMixin):
130130
rtol = 1e-2
131131

132132

133-
def test_group_ohlc():
134-
def _check(dtype):
135-
obj = np.array(np.random.randn(20), dtype=dtype)
133+
@pytest.mark.parametrize("dtype", ["float32", "float64"])
134+
def test_group_ohlc(dtype):
135+
obj = np.array(np.random.randn(20), dtype=dtype)
136136

137-
bins = np.array([6, 12, 20])
138-
out = np.zeros((3, 4), dtype)
139-
counts = np.zeros(len(out), dtype=np.int64)
140-
labels = ensure_platform_int(np.repeat(np.arange(3), np.diff(np.r_[0, bins])))
137+
bins = np.array([6, 12, 20])
138+
out = np.zeros((3, 4), dtype)
139+
counts = np.zeros(len(out), dtype=np.int64)
140+
labels = ensure_platform_int(np.repeat(np.arange(3), np.diff(np.r_[0, bins])))
141141

142-
func = libgroupby.group_ohlc
143-
func(out, counts, obj[:, None], labels)
142+
func = libgroupby.group_ohlc
143+
func(out, counts, obj[:, None], labels)
144144

145-
def _ohlc(group):
146-
if isna(group).all():
147-
return np.repeat(np.nan, 4)
148-
return [group[0], group.max(), group.min(), group[-1]]
145+
def _ohlc(group):
146+
if isna(group).all():
147+
return np.repeat(np.nan, 4)
148+
return [group[0], group.max(), group.min(), group[-1]]
149149

150-
expected = np.array([_ohlc(obj[:6]), _ohlc(obj[6:12]), _ohlc(obj[12:])])
150+
expected = np.array([_ohlc(obj[:6]), _ohlc(obj[6:12]), _ohlc(obj[12:])])
151151

152-
tm.assert_almost_equal(out, expected)
153-
tm.assert_numpy_array_equal(counts, np.array([6, 6, 8], dtype=np.int64))
152+
tm.assert_almost_equal(out, expected)
153+
tm.assert_numpy_array_equal(counts, np.array([6, 6, 8], dtype=np.int64))
154154

155-
obj[:6] = np.nan
156-
func(out, counts, obj[:, None], labels)
157-
expected[0] = np.nan
158-
tm.assert_almost_equal(out, expected)
159-
160-
_check("float32")
161-
_check("float64")
155+
obj[:6] = np.nan
156+
func(out, counts, obj[:, None], labels)
157+
expected[0] = np.nan
158+
tm.assert_almost_equal(out, expected)
162159

163160

164161
def _check_cython_group_transform_cumulative(pd_op, np_op, dtype):

pandas/tests/indexes/multi/test_duplicates.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,49 +178,44 @@ def test_has_duplicates_from_tuples():
178178
assert not mi.has_duplicates
179179

180180

181-
def test_has_duplicates_overflow():
181+
@pytest.mark.parametrize("nlevels", [4, 8])
182+
@pytest.mark.parametrize("with_nulls", [True, False])
183+
def test_has_duplicates_overflow(nlevels, with_nulls):
182184
# handle int64 overflow if possible
183-
def check(nlevels, with_nulls):
184-
codes = np.tile(np.arange(500), 2)
185-
level = np.arange(500)
185+
# no overflow with 4
186+
# overflow possible with 8
187+
codes = np.tile(np.arange(500), 2)
188+
level = np.arange(500)
186189

187-
if with_nulls: # inject some null values
188-
codes[500] = -1 # common nan value
189-
codes = [codes.copy() for i in range(nlevels)]
190-
for i in range(nlevels):
191-
codes[i][500 + i - nlevels // 2] = -1
190+
if with_nulls: # inject some null values
191+
codes[500] = -1 # common nan value
192+
codes = [codes.copy() for i in range(nlevels)]
193+
for i in range(nlevels):
194+
codes[i][500 + i - nlevels // 2] = -1
192195

193-
codes += [np.array([-1, 1]).repeat(500)]
194-
else:
195-
codes = [codes] * nlevels + [np.arange(2).repeat(500)]
196+
codes += [np.array([-1, 1]).repeat(500)]
197+
else:
198+
codes = [codes] * nlevels + [np.arange(2).repeat(500)]
196199

197-
levels = [level] * nlevels + [[0, 1]]
200+
levels = [level] * nlevels + [[0, 1]]
198201

199-
# no dups
200-
mi = MultiIndex(levels=levels, codes=codes)
201-
assert not mi.has_duplicates
202-
203-
# with a dup
204-
if with_nulls:
205-
206-
def f(a):
207-
return np.insert(a, 1000, a[0])
202+
# no dups
203+
mi = MultiIndex(levels=levels, codes=codes)
204+
assert not mi.has_duplicates
208205

209-
codes = list(map(f, codes))
210-
mi = MultiIndex(levels=levels, codes=codes)
211-
else:
212-
values = mi.values.tolist()
213-
mi = MultiIndex.from_tuples(values + [values[0]])
206+
# with a dup
207+
if with_nulls:
214208

215-
assert mi.has_duplicates
209+
def f(a):
210+
return np.insert(a, 1000, a[0])
216211

217-
# no overflow
218-
check(4, False)
219-
check(4, True)
212+
codes = list(map(f, codes))
213+
mi = MultiIndex(levels=levels, codes=codes)
214+
else:
215+
values = mi.values.tolist()
216+
mi = MultiIndex.from_tuples(values + [values[0]])
220217

221-
# overflow possible
222-
check(8, False)
223-
check(8, True)
218+
assert mi.has_duplicates
224219

225220

226221
@pytest.mark.parametrize(

pandas/tests/io/pytables/test_append.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,23 +670,24 @@ def test_append_misc(setup_path):
670670
result = store.select("df1")
671671
tm.assert_frame_equal(result, df)
672672

673-
# more chunksize in append tests
674-
def check(obj, comparator):
675-
for c in [10, 200, 1000]:
676-
with ensure_clean_store(setup_path, mode="w") as store:
677-
store.append("obj", obj, chunksize=c)
678-
result = store.select("obj")
679-
comparator(result, obj)
680673

674+
@pytest.mark.parametrize("chunksize", [10, 200, 1000])
675+
def test_append_misc_chunksize(setup_path, chunksize):
676+
# more chunksize in append tests
681677
df = tm.makeDataFrame()
682678
df["string"] = "foo"
683679
df["float322"] = 1.0
684680
df["float322"] = df["float322"].astype("float32")
685681
df["bool"] = df["float322"] > 0
686682
df["time1"] = Timestamp("20130101")
687683
df["time2"] = Timestamp("20130102")
688-
check(df, tm.assert_frame_equal)
684+
with ensure_clean_store(setup_path, mode="w") as store:
685+
store.append("obj", df, chunksize=chunksize)
686+
result = store.select("obj")
687+
tm.assert_frame_equal(result, df)
688+
689689

690+
def test_append_misc_empty_frame(setup_path):
690691
# empty frame, GH4273
691692
with ensure_clean_store(setup_path) as store:
692693

pandas/tests/io/pytables/test_file_handling.py

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,71 +29,64 @@
2929
pytestmark = pytest.mark.single
3030

3131

32-
def test_mode(setup_path):
32+
@pytest.mark.parametrize("mode", ["r", "r+", "a", "w"])
33+
def test_mode(setup_path, mode):
3334

3435
df = tm.makeTimeDataFrame()
36+
msg = r"[\S]* does not exist"
37+
with ensure_clean_path(setup_path) as path:
3538

36-
def check(mode):
37-
38-
msg = r"[\S]* does not exist"
39-
with ensure_clean_path(setup_path) as path:
40-
41-
# constructor
42-
if mode in ["r", "r+"]:
43-
with pytest.raises(OSError, match=msg):
44-
HDFStore(path, mode=mode)
39+
# constructor
40+
if mode in ["r", "r+"]:
41+
with pytest.raises(OSError, match=msg):
42+
HDFStore(path, mode=mode)
4543

46-
else:
47-
store = HDFStore(path, mode=mode)
48-
assert store._handle.mode == mode
49-
store.close()
44+
else:
45+
store = HDFStore(path, mode=mode)
46+
assert store._handle.mode == mode
47+
store.close()
5048

51-
with ensure_clean_path(setup_path) as path:
49+
with ensure_clean_path(setup_path) as path:
5250

53-
# context
54-
if mode in ["r", "r+"]:
55-
with pytest.raises(OSError, match=msg):
56-
with HDFStore(path, mode=mode) as store:
57-
pass
58-
else:
51+
# context
52+
if mode in ["r", "r+"]:
53+
with pytest.raises(OSError, match=msg):
5954
with HDFStore(path, mode=mode) as store:
60-
assert store._handle.mode == mode
55+
pass
56+
else:
57+
with HDFStore(path, mode=mode) as store:
58+
assert store._handle.mode == mode
6159

62-
with ensure_clean_path(setup_path) as path:
60+
with ensure_clean_path(setup_path) as path:
6361

64-
# conv write
65-
if mode in ["r", "r+"]:
66-
with pytest.raises(OSError, match=msg):
67-
df.to_hdf(path, "df", mode=mode)
68-
df.to_hdf(path, "df", mode="w")
69-
else:
62+
# conv write
63+
if mode in ["r", "r+"]:
64+
with pytest.raises(OSError, match=msg):
7065
df.to_hdf(path, "df", mode=mode)
71-
72-
# conv read
73-
if mode in ["w"]:
74-
msg = (
75-
"mode w is not allowed while performing a read. "
76-
r"Allowed modes are r, r\+ and a."
77-
)
78-
with pytest.raises(ValueError, match=msg):
79-
read_hdf(path, "df", mode=mode)
80-
else:
81-
result = read_hdf(path, "df", mode=mode)
82-
tm.assert_frame_equal(result, df)
83-
84-
def check_default_mode():
85-
86-
# read_hdf uses default mode
87-
with ensure_clean_path(setup_path) as path:
8866
df.to_hdf(path, "df", mode="w")
89-
result = read_hdf(path, "df")
67+
else:
68+
df.to_hdf(path, "df", mode=mode)
69+
70+
# conv read
71+
if mode in ["w"]:
72+
msg = (
73+
"mode w is not allowed while performing a read. "
74+
r"Allowed modes are r, r\+ and a."
75+
)
76+
with pytest.raises(ValueError, match=msg):
77+
read_hdf(path, "df", mode=mode)
78+
else:
79+
result = read_hdf(path, "df", mode=mode)
9080
tm.assert_frame_equal(result, df)
9181

92-
check("r")
93-
check("r+")
94-
check("a")
95-
check("w")
96-
check_default_mode()
82+
83+
def test_default_mode(setup_path):
84+
# read_hdf uses default mode
85+
df = tm.makeTimeDataFrame()
86+
with ensure_clean_path(setup_path) as path:
87+
df.to_hdf(path, "df", mode="w")
88+
result = read_hdf(path, "df")
89+
tm.assert_frame_equal(result, df)
9790

9891

9992
def test_reopen_handle(setup_path):

0 commit comments

Comments
 (0)