Skip to content

Commit e657c30

Browse files
authored
TST: refactored tests to use parametrize instead of "for" loop (#41195)
1 parent 5ed8e3a commit e657c30

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

pandas/tests/generic/test_series.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,46 @@ def test_nonzero_single_element(self):
5252
s = Series([False])
5353
assert not s.bool()
5454

55-
msg = "The truth value of a Series is ambiguous"
55+
@pytest.mark.parametrize("data", [np.nan, pd.NaT, True, False])
56+
def test_nonzero_single_element_raise_1(self, data):
5657
# single item nan to raise
57-
for s in [Series([np.nan]), Series([pd.NaT]), Series([True]), Series([False])]:
58-
with pytest.raises(ValueError, match=msg):
59-
bool(s)
58+
series = Series([data])
59+
60+
msg = "The truth value of a Series is ambiguous"
61+
with pytest.raises(ValueError, match=msg):
62+
bool(series)
63+
64+
@pytest.mark.parametrize("data", [np.nan, pd.NaT])
65+
def test_nonzero_single_element_raise_2(self, data):
66+
series = Series([data])
6067

6168
msg = "bool cannot act on a non-boolean single element Series"
62-
for s in [Series([np.nan]), Series([pd.NaT])]:
63-
with pytest.raises(ValueError, match=msg):
64-
s.bool()
69+
with pytest.raises(ValueError, match=msg):
70+
series.bool()
6571

72+
@pytest.mark.parametrize("data", [(True, True), (False, False)])
73+
def test_nonzero_multiple_element_raise(self, data):
6674
# multiple bool are still an error
75+
series = Series([data])
76+
6777
msg = "The truth value of a Series is ambiguous"
68-
for s in [Series([True, True]), Series([False, False])]:
69-
with pytest.raises(ValueError, match=msg):
70-
bool(s)
71-
with pytest.raises(ValueError, match=msg):
72-
s.bool()
78+
with pytest.raises(ValueError, match=msg):
79+
bool(series)
80+
with pytest.raises(ValueError, match=msg):
81+
series.bool()
7382

83+
@pytest.mark.parametrize("data", [1, 0, "a", 0.0])
84+
def test_nonbool_single_element_raise(self, data):
7485
# single non-bool are an error
75-
for s in [Series([1]), Series([0]), Series(["a"]), Series([0.0])]:
76-
msg = "The truth value of a Series is ambiguous"
77-
with pytest.raises(ValueError, match=msg):
78-
bool(s)
79-
msg = "bool cannot act on a non-boolean single element Series"
80-
with pytest.raises(ValueError, match=msg):
81-
s.bool()
86+
series = Series([data])
87+
88+
msg = "The truth value of a Series is ambiguous"
89+
with pytest.raises(ValueError, match=msg):
90+
bool(series)
91+
92+
msg = "bool cannot act on a non-boolean single element Series"
93+
with pytest.raises(ValueError, match=msg):
94+
series.bool()
8295

8396
def test_metadata_propagation_indiv_resample(self):
8497
# resample

0 commit comments

Comments
 (0)