diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index fe4748eb0eba0..039e0df4193b3 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -686,9 +686,9 @@ def maybe_convert_numeric(object[:] values, set na_values, raise ValueError('integer out of range') else: seen_float = True - except: + except (TypeError, ValueError) as e: if not coerce_numeric: - raise + raise type(e)(str(e) + ' at position {}'.format(i)) floats[i] = nan seen_float = True diff --git a/pandas/src/parse_helper.h b/pandas/src/parse_helper.h index fd5089dd8963d..e565f02f27c88 100644 --- a/pandas/src/parse_helper.h +++ b/pandas/src/parse_helper.h @@ -66,7 +66,7 @@ int floatify(PyObject* str, double *result, int *maybe_int) { return 0; parsingerror: - PyErr_SetString(PyExc_ValueError, "Unable to parse string"); + PyErr_Format(PyExc_ValueError, "Unable to parse string \"%s\"", data); Py_XDECREF(tmp); return -1; diff --git a/pandas/tools/tests/test_util.py b/pandas/tools/tests/test_util.py index 5b738086a1ad4..7532997ef9d8e 100644 --- a/pandas/tools/tests/test_util.py +++ b/pandas/tools/tests/test_util.py @@ -120,7 +120,8 @@ def test_series_numeric(self): def test_error(self): s = pd.Series([1, -3.14, 'apple']) - with tm.assertRaises(ValueError): + msg = 'Unable to parse string "apple" at position 2' + with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') res = to_numeric(s, errors='ignore') @@ -131,9 +132,15 @@ def test_error(self): expected = pd.Series([1, -3.14, np.nan]) tm.assert_series_equal(res, expected) + s = pd.Series(['orange', 1, -3.14, 'apple']) + msg = 'Unable to parse string "orange" at position 0' + with tm.assertRaisesRegexp(ValueError, msg): + to_numeric(s, errors='raise') + def test_error_seen_bool(self): s = pd.Series([True, False, 'apple']) - with tm.assertRaises(ValueError): + msg = 'Unable to parse string "apple" at position 2' + with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') res = to_numeric(s, errors='ignore')