|
7 | 7 | import numpy as np
|
8 | 8 | import pytest
|
9 | 9 |
|
10 |
| -from pandas.compat import PY3, range, zip |
| 10 | +from pandas.compat import PY2, PY3, range, zip |
11 | 11 |
|
12 | 12 | from pandas.core.dtypes.common import is_scalar
|
13 | 13 |
|
|
16 | 16 | import pandas.util.testing as tm
|
17 | 17 | from pandas.util.testing import assert_frame_equal, assert_series_equal
|
18 | 18 |
|
19 |
| -import pandas.io.formats.printing as printing |
20 |
| - |
21 | 19 | # ----------------------------------------------------------------------
|
22 | 20 | # Generic types test cases
|
23 | 21 |
|
@@ -135,37 +133,51 @@ def test_nonzero(self):
|
135 | 133 | # GH 4633
|
136 | 134 | # look at the boolean/nonzero behavior for objects
|
137 | 135 | obj = self._construct(shape=4)
|
138 |
| - pytest.raises(ValueError, lambda: bool(obj == 0)) |
139 |
| - pytest.raises(ValueError, lambda: bool(obj == 1)) |
140 |
| - pytest.raises(ValueError, lambda: bool(obj)) |
| 136 | + msg = "The truth value of a {} is ambiguous".format( |
| 137 | + self._typ.__name__) |
| 138 | + with pytest.raises(ValueError, match=msg): |
| 139 | + bool(obj == 0) |
| 140 | + with pytest.raises(ValueError, match=msg): |
| 141 | + bool(obj == 1) |
| 142 | + with pytest.raises(ValueError, match=msg): |
| 143 | + bool(obj) |
141 | 144 |
|
142 | 145 | obj = self._construct(shape=4, value=1)
|
143 |
| - pytest.raises(ValueError, lambda: bool(obj == 0)) |
144 |
| - pytest.raises(ValueError, lambda: bool(obj == 1)) |
145 |
| - pytest.raises(ValueError, lambda: bool(obj)) |
| 146 | + with pytest.raises(ValueError, match=msg): |
| 147 | + bool(obj == 0) |
| 148 | + with pytest.raises(ValueError, match=msg): |
| 149 | + bool(obj == 1) |
| 150 | + with pytest.raises(ValueError, match=msg): |
| 151 | + bool(obj) |
146 | 152 |
|
147 | 153 | obj = self._construct(shape=4, value=np.nan)
|
148 |
| - pytest.raises(ValueError, lambda: bool(obj == 0)) |
149 |
| - pytest.raises(ValueError, lambda: bool(obj == 1)) |
150 |
| - pytest.raises(ValueError, lambda: bool(obj)) |
| 154 | + with pytest.raises(ValueError, match=msg): |
| 155 | + bool(obj == 0) |
| 156 | + with pytest.raises(ValueError, match=msg): |
| 157 | + bool(obj == 1) |
| 158 | + with pytest.raises(ValueError, match=msg): |
| 159 | + bool(obj) |
151 | 160 |
|
152 | 161 | # empty
|
153 | 162 | obj = self._construct(shape=0)
|
154 |
| - pytest.raises(ValueError, lambda: bool(obj)) |
| 163 | + with pytest.raises(ValueError, match=msg): |
| 164 | + bool(obj) |
155 | 165 |
|
156 | 166 | # invalid behaviors
|
157 | 167 |
|
158 | 168 | obj1 = self._construct(shape=4, value=1)
|
159 | 169 | obj2 = self._construct(shape=4, value=1)
|
160 | 170 |
|
161 |
| - def f(): |
| 171 | + with pytest.raises(ValueError, match=msg): |
162 | 172 | if obj1:
|
163 |
| - printing.pprint_thing("this works and shouldn't") |
| 173 | + pass |
164 | 174 |
|
165 |
| - pytest.raises(ValueError, f) |
166 |
| - pytest.raises(ValueError, lambda: obj1 and obj2) |
167 |
| - pytest.raises(ValueError, lambda: obj1 or obj2) |
168 |
| - pytest.raises(ValueError, lambda: not obj1) |
| 175 | + with pytest.raises(ValueError, match=msg): |
| 176 | + obj1 and obj2 |
| 177 | + with pytest.raises(ValueError, match=msg): |
| 178 | + obj1 or obj2 |
| 179 | + with pytest.raises(ValueError, match=msg): |
| 180 | + not obj1 |
169 | 181 |
|
170 | 182 | def test_downcast(self):
|
171 | 183 | # test close downcasting
|
@@ -200,9 +212,10 @@ def test_constructor_compound_dtypes(self):
|
200 | 212 | def f(dtype):
|
201 | 213 | return self._construct(shape=3, value=1, dtype=dtype)
|
202 | 214 |
|
203 |
| - pytest.raises(NotImplementedError, f, [("A", "datetime64[h]"), |
204 |
| - ("B", "str"), |
205 |
| - ("C", "int32")]) |
| 215 | + msg = ("compound dtypes are not implemented in the {} constructor" |
| 216 | + .format(self._typ.__name__)) |
| 217 | + with pytest.raises(NotImplementedError, match=msg): |
| 218 | + f([("A", "datetime64[h]"), ("B", "str"), ("C", "int32")]) |
206 | 219 |
|
207 | 220 | # these work (though results may be unexpected)
|
208 | 221 | f('int64')
|
@@ -725,6 +738,7 @@ def test_sample(sel):
|
725 | 738 | with pytest.raises(ValueError):
|
726 | 739 | df.sample(1, weights=s4)
|
727 | 740 |
|
| 741 | + @pytest.mark.skipif(PY2, reason="pytest.raises match regex fails") |
728 | 742 | def test_squeeze(self):
|
729 | 743 | # noop
|
730 | 744 | for s in [tm.makeFloatSeries(), tm.makeStringSeries(),
|
@@ -755,8 +769,14 @@ def test_squeeze(self):
|
755 | 769 | tm.assert_series_equal(df.squeeze(axis=1), df.iloc[:, 0])
|
756 | 770 | tm.assert_series_equal(df.squeeze(axis='columns'), df.iloc[:, 0])
|
757 | 771 | assert df.squeeze() == df.iloc[0, 0]
|
758 |
| - pytest.raises(ValueError, df.squeeze, axis=2) |
759 |
| - pytest.raises(ValueError, df.squeeze, axis='x') |
| 772 | + msg = ("No axis named 2 for object type <class" |
| 773 | + " 'pandas.core.frame.DataFrame'>") |
| 774 | + with pytest.raises(ValueError, match=msg): |
| 775 | + df.squeeze(axis=2) |
| 776 | + msg = ("No axis named x for object type <class" |
| 777 | + " 'pandas.core.frame.DataFrame'>") |
| 778 | + with pytest.raises(ValueError, match=msg): |
| 779 | + df.squeeze(axis='x') |
760 | 780 |
|
761 | 781 | df = tm.makeTimeDataFrame(3)
|
762 | 782 | tm.assert_frame_equal(df.squeeze(axis=0), df)
|
|
0 commit comments