|
25 | 25 | from pandas import date_range
|
26 | 26 | import pandas as pd
|
27 | 27 | from pandas.io.parsers import read_csv
|
| 28 | +from pandas.parser import CParserError |
28 | 29 |
|
29 | 30 | from pandas.util.testing import (assert_almost_equal,
|
30 | 31 | assert_series_equal,
|
31 | 32 | assert_frame_equal,
|
| 33 | + assertRaisesRegexp, |
32 | 34 | makeCustomDataframe as mkdf,
|
33 | 35 | ensure_clean)
|
34 | 36 | from pandas.util import py3compat
|
@@ -2208,60 +2210,45 @@ def test_constructor_dict(self):
|
2208 | 2210 | def test_constructor_error_msgs(self):
|
2209 | 2211 |
|
2210 | 2212 | # mix dict and array, wrong size
|
2211 |
| - try: |
| 2213 | + def testit(): |
2212 | 2214 | DataFrame({'A': {'a': 'a', 'b': 'b'},
|
2213 | 2215 | 'B': ['a', 'b', 'c']})
|
2214 |
| - except (Exception), detail: |
2215 |
| - self.assert_(type(detail) == ValueError) |
2216 |
| - self.assert_("Mixing dicts with non-Series may lead to ambiguous ordering." in str(detail)) |
| 2216 | + assertRaisesRegexp(ValueError, "Mixing dicts with non-Series may lead to ambiguous ordering.", testit) |
2217 | 2217 |
|
2218 | 2218 | # wrong size ndarray, GH 3105
|
2219 |
| - try: |
| 2219 | + def testit(): |
2220 | 2220 | DataFrame(np.arange(12).reshape((4, 3)), columns=['foo', 'bar', 'baz'],
|
2221 | 2221 | index=date_range('2000-01-01', periods=3))
|
2222 |
| - except (Exception), detail: |
2223 |
| - self.assert_(type(detail) == ValueError) |
2224 |
| - self.assert_(str(detail).startswith("Shape of passed values is (3, 4), indices imply (3, 3)")) |
| 2222 | + assertRaisesRegexp(ValueError, "Shape of passed values is \(3, 4\), indices imply \(3, 3\)", testit) |
2225 | 2223 |
|
2226 | 2224 | # higher dim raise exception
|
2227 |
| - try: |
| 2225 | + def testit(): |
2228 | 2226 | DataFrame(np.zeros((3, 3, 3)), columns=['A', 'B', 'C'], index=[1])
|
2229 |
| - except (Exception), detail: |
2230 |
| - self.assert_(type(detail) == ValueError) |
2231 |
| - self.assert_("Must pass 2-d input" in str(detail)) |
| 2227 | + assertRaisesRegexp(ValueError, "Must pass 2-d input", testit) |
2232 | 2228 |
|
2233 | 2229 | # wrong size axis labels
|
2234 |
| - try: |
| 2230 | + def testit(): |
2235 | 2231 | DataFrame(np.random.rand(2,3), columns=['A', 'B', 'C'], index=[1])
|
2236 |
| - except (Exception), detail: |
2237 |
| - self.assert_(type(detail) == ValueError) |
2238 |
| - self.assert_(str(detail).startswith("Shape of passed values is (3, 2), indices imply (3, 1)")) |
| 2232 | + assertRaisesRegexp(ValueError, "Shape of passed values is \(3, 2\), indices imply \(3, 1\)", testit) |
2239 | 2233 |
|
2240 |
| - try: |
| 2234 | + def testit(): |
2241 | 2235 | DataFrame(np.random.rand(2,3), columns=['A', 'B'], index=[1, 2])
|
2242 |
| - except (Exception), detail: |
2243 |
| - self.assert_(type(detail) == ValueError) |
2244 |
| - self.assert_(str(detail).startswith("Shape of passed values is (3, 2), indices imply (2, 2)")) |
| 2236 | + assertRaisesRegexp(ValueError, "Shape of passed values is \(3, 2\), indices imply \(2, 2\)", testit) |
2245 | 2237 |
|
2246 |
| - try: |
| 2238 | + def testit(): |
2247 | 2239 | DataFrame({'a': False, 'b': True})
|
2248 |
| - except (Exception), detail: |
2249 |
| - msg = 'If using all scalar values, you must must pass an index' |
2250 |
| - self.assert_(type(detail) == ValueError) |
2251 |
| - self.assert_(msg in str(detail)) |
| 2240 | + assertRaisesRegexp(ValueError, 'If using all scalar values, you must must pass an index', testit) |
2252 | 2241 |
|
2253 | 2242 | def test_insert_error_msmgs(self):
|
2254 | 2243 |
|
2255 | 2244 | # GH 4107, more descriptive error message
|
2256 | 2245 | df = DataFrame(np.random.randint(0,2,(4,4)),
|
2257 | 2246 | columns=['a', 'b', 'c', 'd'])
|
2258 | 2247 |
|
2259 |
| - try: |
| 2248 | + def testit(): |
2260 | 2249 | df['gr'] = df.groupby(['b', 'c']).count()
|
2261 |
| - except (Exception), detail: |
2262 |
| - msg = 'incompatible index of inserted column with frame index' |
2263 |
| - self.assert_(type(detail) == TypeError) |
2264 |
| - self.assert_(msg in str(detail)) |
| 2250 | + |
| 2251 | + assertRaisesRegexp(TypeError, 'incompatible index of inserted column with frame index', testit) |
2265 | 2252 |
|
2266 | 2253 | def test_constructor_subclass_dict(self):
|
2267 | 2254 | # Test for passing dict subclass to constructor
|
@@ -5133,17 +5120,13 @@ def _make_frame(names=None):
|
5133 | 5120 | df.to_csv(path,tupleize_cols=False)
|
5134 | 5121 |
|
5135 | 5122 | # catch invalid headers
|
5136 |
| - try: |
| 5123 | + def testit(): |
5137 | 5124 | read_csv(path,tupleize_cols=False,header=range(3),index_col=0)
|
5138 |
| - except (Exception), detail: |
5139 |
| - if not str(detail).startswith('Passed header=[0,1,2] are too many rows for this multi_index of columns'): |
5140 |
| - raise AssertionError("failure in read_csv header=range(3)") |
| 5125 | + assertRaisesRegexp(CParserError, 'Passed header=\[0,1,2\] are too many rows for this multi_index of columns', testit) |
5141 | 5126 |
|
5142 |
| - try: |
| 5127 | + def testit(): |
5143 | 5128 | read_csv(path,tupleize_cols=False,header=range(7),index_col=0)
|
5144 |
| - except (Exception), detail: |
5145 |
| - if not str(detail).startswith('Passed header=[0,1,2,3,4,5,6], len of 7, but only 6 lines in file'): |
5146 |
| - raise AssertionError("failure in read_csv header=range(7)") |
| 5129 | + assertRaisesRegexp(CParserError, 'Passed header=\[0,1,2,3,4,5,6\], len of 7, but only 6 lines in file', testit) |
5147 | 5130 |
|
5148 | 5131 | for i in [3,4,5,6,7]:
|
5149 | 5132 | self.assertRaises(Exception, read_csv, path, tupleize_cols=False, header=range(i), index_col=0)
|
|
0 commit comments