Skip to content

Commit 9a75359

Browse files
authored
TST: Parameterize tests (pandas-dev#45862)
1 parent 6a5cdf5 commit 9a75359

File tree

7 files changed

+111
-122
lines changed

7 files changed

+111
-122
lines changed

pandas/tests/arithmetic/test_timedelta64.py

+1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ def test_timedelta(self, freq):
538538
expected = index + timedelta(-1)
539539
tm.assert_index_equal(result, expected)
540540

541+
def test_timedelta_tick_arithmetic(self):
541542
# GH#4134, buggy with timedeltas
542543
rng = pd.date_range("2013", "2014")
543544
s = Series(rng)

pandas/tests/arrays/sparse/test_indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
class TestGetitem:
1616
def test_getitem(self):
17-
def _checkit(i):
18-
tm.assert_almost_equal(arr[i], arr.to_dense()[i])
19-
17+
dense = arr.to_dense()
2018
for i in range(len(arr)):
21-
_checkit(i)
22-
_checkit(-i)
19+
tm.assert_almost_equal(arr[i], dense[i])
20+
tm.assert_almost_equal(arr[-i], dense[-i])
2321

2422
def test_getitem_arraylike_mask(self):
2523
arr = SparseArray([0, 1, 2])

pandas/tests/frame/indexing/test_indexing.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,14 @@ def test_getitem_boolean_casting(self, datetime_frame):
195195
)
196196
tm.assert_series_equal(result, expected)
197197

198-
def test_getitem_boolean_list(self):
198+
@pytest.mark.parametrize(
199+
"lst", [[True, False, True], [True, True, True], [False, False, False]]
200+
)
201+
def test_getitem_boolean_list(self, lst):
199202
df = DataFrame(np.arange(12).reshape(3, 4))
200-
201-
def _checkit(lst):
202-
result = df[lst]
203-
expected = df.loc[df.index[lst]]
204-
tm.assert_frame_equal(result, expected)
205-
206-
_checkit([True, False, True])
207-
_checkit([True, True, True])
208-
_checkit([False, False, False])
203+
result = df[lst]
204+
expected = df.loc[df.index[lst]]
205+
tm.assert_frame_equal(result, expected)
209206

210207
def test_getitem_boolean_iadd(self):
211208
arr = np.random.randn(5, 5)

pandas/tests/frame/test_arithmetic.py

+5-29
Original file line numberDiff line numberDiff line change
@@ -847,36 +847,12 @@ def test_arith_mixed(self):
847847
expected = DataFrame({"A": ["aa", "bb", "cc"], "B": [2, 4, 6]})
848848
tm.assert_frame_equal(result, expected)
849849

850-
def test_arith_getitem_commute(self):
850+
@pytest.mark.parametrize("col", ["A", "B"])
851+
def test_arith_getitem_commute(self, all_arithmetic_functions, col):
851852
df = DataFrame({"A": [1.1, 3.3], "B": [2.5, -3.9]})
852-
853-
def _test_op(df, op):
854-
result = op(df, 1)
855-
856-
if not df.columns.is_unique:
857-
raise ValueError("Only unique columns supported by this test")
858-
859-
for col in result.columns:
860-
tm.assert_series_equal(result[col], op(df[col], 1))
861-
862-
_test_op(df, operator.add)
863-
_test_op(df, operator.sub)
864-
_test_op(df, operator.mul)
865-
_test_op(df, operator.truediv)
866-
_test_op(df, operator.floordiv)
867-
_test_op(df, operator.pow)
868-
869-
_test_op(df, lambda x, y: y + x)
870-
_test_op(df, lambda x, y: y - x)
871-
_test_op(df, lambda x, y: y * x)
872-
_test_op(df, lambda x, y: y / x)
873-
_test_op(df, lambda x, y: y**x)
874-
875-
_test_op(df, lambda x, y: x + y)
876-
_test_op(df, lambda x, y: x - y)
877-
_test_op(df, lambda x, y: x * y)
878-
_test_op(df, lambda x, y: x / y)
879-
_test_op(df, lambda x, y: x**y)
853+
result = all_arithmetic_functions(df, 1)[col]
854+
expected = all_arithmetic_functions(df[col], 1)
855+
tm.assert_series_equal(result, expected)
880856

881857
@pytest.mark.parametrize(
882858
"values", [[1, 2], (1, 2), np.array([1, 2]), range(1, 3), deque([1, 2])]

pandas/tests/frame/test_stack_unstack.py

+52-39
Original file line numberDiff line numberDiff line change
@@ -992,40 +992,9 @@ def test_stack_datetime_column_multiIndex(self):
992992
expected = DataFrame([1, 2, 3, 4], index=eidx, columns=ecols)
993993
tm.assert_frame_equal(result, expected)
994994

995-
def test_stack_partial_multiIndex(self):
996-
# GH 8844
997-
def _test_stack_with_multiindex(multiindex):
998-
df = DataFrame(
999-
np.arange(3 * len(multiindex)).reshape(3, len(multiindex)),
1000-
columns=multiindex,
1001-
)
1002-
for level in (-1, 0, 1, [0, 1], [1, 0]):
1003-
result = df.stack(level=level, dropna=False)
1004-
1005-
if isinstance(level, int):
1006-
# Stacking a single level should not make any all-NaN rows,
1007-
# so df.stack(level=level, dropna=False) should be the same
1008-
# as df.stack(level=level, dropna=True).
1009-
expected = df.stack(level=level, dropna=True)
1010-
if isinstance(expected, Series):
1011-
tm.assert_series_equal(result, expected)
1012-
else:
1013-
tm.assert_frame_equal(result, expected)
1014-
1015-
df.columns = MultiIndex.from_tuples(
1016-
df.columns.to_numpy(), names=df.columns.names
1017-
)
1018-
expected = df.stack(level=level, dropna=False)
1019-
if isinstance(expected, Series):
1020-
tm.assert_series_equal(result, expected)
1021-
else:
1022-
tm.assert_frame_equal(result, expected)
1023-
1024-
full_multiindex = MultiIndex.from_tuples(
1025-
[("B", "x"), ("B", "z"), ("A", "y"), ("C", "x"), ("C", "u")],
1026-
names=["Upper", "Lower"],
1027-
)
1028-
for multiindex_columns in (
995+
@pytest.mark.parametrize(
996+
"multiindex_columns",
997+
[
1029998
[0, 1, 2, 3, 4],
1030999
[0, 1, 2, 3],
10311000
[0, 1, 2, 4],
@@ -1038,12 +1007,56 @@ def _test_stack_with_multiindex(multiindex):
10381007
[0],
10391008
[2],
10401009
[4],
1041-
):
1042-
_test_stack_with_multiindex(full_multiindex[multiindex_columns])
1043-
if len(multiindex_columns) > 1:
1044-
multiindex_columns.reverse()
1045-
_test_stack_with_multiindex(full_multiindex[multiindex_columns])
1010+
[4, 3, 2, 1, 0],
1011+
[3, 2, 1, 0],
1012+
[4, 2, 1, 0],
1013+
[2, 1, 0],
1014+
[3, 2, 1],
1015+
[4, 3, 2],
1016+
[1, 0],
1017+
[2, 0],
1018+
[3, 0],
1019+
],
1020+
)
1021+
@pytest.mark.parametrize("level", (-1, 0, 1, [0, 1], [1, 0]))
1022+
def test_stack_partial_multiIndex(self, multiindex_columns, level):
1023+
# GH 8844
1024+
full_multiindex = MultiIndex.from_tuples(
1025+
[("B", "x"), ("B", "z"), ("A", "y"), ("C", "x"), ("C", "u")],
1026+
names=["Upper", "Lower"],
1027+
)
1028+
multiindex = full_multiindex[multiindex_columns]
1029+
df = DataFrame(
1030+
np.arange(3 * len(multiindex)).reshape(3, len(multiindex)),
1031+
columns=multiindex,
1032+
)
1033+
result = df.stack(level=level, dropna=False)
1034+
1035+
if isinstance(level, int):
1036+
# Stacking a single level should not make any all-NaN rows,
1037+
# so df.stack(level=level, dropna=False) should be the same
1038+
# as df.stack(level=level, dropna=True).
1039+
expected = df.stack(level=level, dropna=True)
1040+
if isinstance(expected, Series):
1041+
tm.assert_series_equal(result, expected)
1042+
else:
1043+
tm.assert_frame_equal(result, expected)
1044+
1045+
df.columns = MultiIndex.from_tuples(
1046+
df.columns.to_numpy(), names=df.columns.names
1047+
)
1048+
expected = df.stack(level=level, dropna=False)
1049+
if isinstance(expected, Series):
1050+
tm.assert_series_equal(result, expected)
1051+
else:
1052+
tm.assert_frame_equal(result, expected)
10461053

1054+
def test_stack_full_multiIndex(self):
1055+
# GH 8844
1056+
full_multiindex = MultiIndex.from_tuples(
1057+
[("B", "x"), ("B", "z"), ("A", "y"), ("C", "x"), ("C", "u")],
1058+
names=["Upper", "Lower"],
1059+
)
10471060
df = DataFrame(np.arange(6).reshape(2, 3), columns=full_multiindex[[0, 1, 3]])
10481061
result = df.stack(dropna=False)
10491062
expected = DataFrame(

pandas/tests/indexes/period/test_tools.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@ class TestPeriodRepresentation:
1414
Wish to match NumPy units
1515
"""
1616

17-
def _check_freq(self, freq, base_date):
17+
@pytest.mark.parametrize(
18+
"freq, base_date",
19+
[
20+
("W-THU", "1970-01-01"),
21+
("D", "1970-01-01"),
22+
("B", "1970-01-01"),
23+
("H", "1970-01-01"),
24+
("T", "1970-01-01"),
25+
("S", "1970-01-01"),
26+
("L", "1970-01-01"),
27+
("U", "1970-01-01"),
28+
("N", "1970-01-01"),
29+
("M", "1970-01"),
30+
("A", 1970),
31+
],
32+
)
33+
def test_freq(self, freq, base_date):
1834
rng = period_range(start=base_date, periods=10, freq=freq)
1935
exp = np.arange(10, dtype=np.int64)
2036

2137
tm.assert_numpy_array_equal(rng.asi8, exp)
2238

23-
def test_annual(self):
24-
self._check_freq("A", 1970)
25-
26-
def test_monthly(self):
27-
self._check_freq("M", "1970-01")
28-
29-
@pytest.mark.parametrize("freq", ["W-THU", "D", "B", "H", "T", "S", "L", "U", "N"])
30-
def test_freq(self, freq):
31-
self._check_freq(freq, "1970-01-01")
32-
3339

3440
class TestPeriodIndexConversion:
3541
def test_tolist(self):

pandas/tests/io/parser/test_textreader.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -261,34 +261,32 @@ def _make_reader(**kwds):
261261
assert (result[1] == exp[1]).all()
262262
assert (result[2] == exp[2]).all()
263263

264-
def test_cr_delimited(self):
265-
def _test(text, **kwargs):
266-
nice_text = text.replace("\r", "\r\n")
267-
result = TextReader(StringIO(text), **kwargs).read()
268-
expected = TextReader(StringIO(nice_text), **kwargs).read()
269-
assert_array_dicts_equal(result, expected)
270-
271-
data = "a,b,c\r1,2,3\r4,5,6\r7,8,9\r10,11,12"
272-
_test(data, delimiter=",")
273-
274-
data = "a b c\r1 2 3\r4 5 6\r7 8 9\r10 11 12"
275-
_test(data, delim_whitespace=True)
276-
277-
data = "a,b,c\r1,2,3\r4,5,6\r,88,9\r10,11,12"
278-
_test(data, delimiter=",")
279-
280-
sample = (
281-
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O\r"
282-
"AAAAA,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0\r"
283-
",BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0"
284-
)
285-
_test(sample, delimiter=",")
286-
287-
data = "A B C\r 2 3\r4 5 6"
288-
_test(data, delim_whitespace=True)
289-
290-
data = "A B C\r2 3\r4 5 6"
291-
_test(data, delim_whitespace=True)
264+
@pytest.mark.parametrize(
265+
"text, kwargs",
266+
[
267+
("a,b,c\r1,2,3\r4,5,6\r7,8,9\r10,11,12", {"delimiter": ","}),
268+
(
269+
"a b c\r1 2 3\r4 5 6\r7 8 9\r10 11 12",
270+
{"delim_whitespace": True},
271+
),
272+
("a,b,c\r1,2,3\r4,5,6\r,88,9\r10,11,12", {"delimiter": ","}),
273+
(
274+
(
275+
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O\r"
276+
"AAAAA,BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0\r"
277+
",BBBBB,0,0,0,0,0,0,0,0,0,0,0,0,0"
278+
),
279+
{"delimiter": ","},
280+
),
281+
("A B C\r 2 3\r4 5 6", {"delim_whitespace": True}),
282+
("A B C\r2 3\r4 5 6", {"delim_whitespace": True}),
283+
],
284+
)
285+
def test_cr_delimited(self, text, kwargs):
286+
nice_text = text.replace("\r", "\r\n")
287+
result = TextReader(StringIO(text), **kwargs).read()
288+
expected = TextReader(StringIO(nice_text), **kwargs).read()
289+
assert_array_dicts_equal(result, expected)
292290

293291
def test_empty_field_eof(self):
294292
data = "a,b,c\n1,2,3\n4,,"

0 commit comments

Comments
 (0)