From e08c587ce4f122a01a935467166d67de8dba3015 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 24 Jul 2016 09:18:25 -0500 Subject: [PATCH 1/4] DOC: show failing string on numeric parse --- pandas/src/inference.pyx | 2 +- pandas/src/parse_helper.h | 2 +- pandas/tools/tests/test_util.py | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index fe4748eb0eba0..418b7d2a1435d 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -686,7 +686,7 @@ def maybe_convert_numeric(object[:] values, set na_values, raise ValueError('integer out of range') else: seen_float = True - except: + except (TypeError, ValueError): if not coerce_numeric: raise 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..4eab2efdfea63 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"' + 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([1, -3.14, 'apple', 'orange']) + msg = 'Unable to parse string "apple"' + 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"' + with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') res = to_numeric(s, errors='ignore') From 7de7c9a64afcd50517a5413c7b65c9d05e35d5af Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 24 Jul 2016 09:55:21 -0500 Subject: [PATCH 2/4] also show row number --- pandas/src/inference.pyx | 4 ++-- pandas/tools/tests/test_util.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index 418b7d2a1435d..d86c587340b1f 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 (TypeError, ValueError): + except (TypeError, ValueError) as e: if not coerce_numeric: - raise + raise type(e)(e.message + ' in row {}'.format(i)) floats[i] = nan seen_float = True diff --git a/pandas/tools/tests/test_util.py b/pandas/tools/tests/test_util.py index 4eab2efdfea63..55b31570a8809 100644 --- a/pandas/tools/tests/test_util.py +++ b/pandas/tools/tests/test_util.py @@ -120,7 +120,7 @@ def test_series_numeric(self): def test_error(self): s = pd.Series([1, -3.14, 'apple']) - msg = 'Unable to parse string "apple"' + msg = 'Unable to parse string "apple" in row 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') @@ -132,14 +132,14 @@ def test_error(self): expected = pd.Series([1, -3.14, np.nan]) tm.assert_series_equal(res, expected) - s = pd.Series([1, -3.14, 'apple', 'orange']) - msg = 'Unable to parse string "apple"' + s = pd.Series(['orange', 1, -3.14, 'apple']) + msg = 'Unable to parse string "orange" in row 0' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') def test_error_seen_bool(self): s = pd.Series([True, False, 'apple']) - msg = 'Unable to parse string "apple"' + msg = 'Unable to parse string "apple" in row 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') From f26725757440c12ca22ef274565beb24298e27a3 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 25 Jul 2016 20:07:53 -0500 Subject: [PATCH 3/4] fix py3 change row message --- pandas/src/inference.pyx | 2 +- pandas/tools/tests/test_util.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index d86c587340b1f..745fa3fbbfdec 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -688,7 +688,7 @@ def maybe_convert_numeric(object[:] values, set na_values, seen_float = True except (TypeError, ValueError) as e: if not coerce_numeric: - raise type(e)(e.message + ' in row {}'.format(i)) + raise type(e)(str(e) + ' at array index {}'.format(i)) floats[i] = nan seen_float = True diff --git a/pandas/tools/tests/test_util.py b/pandas/tools/tests/test_util.py index 55b31570a8809..bc388d71310ef 100644 --- a/pandas/tools/tests/test_util.py +++ b/pandas/tools/tests/test_util.py @@ -120,7 +120,7 @@ def test_series_numeric(self): def test_error(self): s = pd.Series([1, -3.14, 'apple']) - msg = 'Unable to parse string "apple" in row 2' + msg = 'Unable to parse string "apple" at array index 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') @@ -133,13 +133,13 @@ def test_error(self): tm.assert_series_equal(res, expected) s = pd.Series(['orange', 1, -3.14, 'apple']) - msg = 'Unable to parse string "orange" in row 0' + msg = 'Unable to parse string "orange" at array index 0' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') def test_error_seen_bool(self): s = pd.Series([True, False, 'apple']) - msg = 'Unable to parse string "apple" in row 2' + msg = 'Unable to parse string "apple" at array index 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') From 13bfd116bbb421bad5154ffd0eff6e8c2de37974 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 26 Jul 2016 06:54:18 -0500 Subject: [PATCH 4/4] call row position --- pandas/src/inference.pyx | 2 +- pandas/tools/tests/test_util.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index 745fa3fbbfdec..039e0df4193b3 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -688,7 +688,7 @@ def maybe_convert_numeric(object[:] values, set na_values, seen_float = True except (TypeError, ValueError) as e: if not coerce_numeric: - raise type(e)(str(e) + ' at array index {}'.format(i)) + raise type(e)(str(e) + ' at position {}'.format(i)) floats[i] = nan seen_float = True diff --git a/pandas/tools/tests/test_util.py b/pandas/tools/tests/test_util.py index bc388d71310ef..7532997ef9d8e 100644 --- a/pandas/tools/tests/test_util.py +++ b/pandas/tools/tests/test_util.py @@ -120,7 +120,7 @@ def test_series_numeric(self): def test_error(self): s = pd.Series([1, -3.14, 'apple']) - msg = 'Unable to parse string "apple" at array index 2' + msg = 'Unable to parse string "apple" at position 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise') @@ -133,13 +133,13 @@ def test_error(self): tm.assert_series_equal(res, expected) s = pd.Series(['orange', 1, -3.14, 'apple']) - msg = 'Unable to parse string "orange" at array index 0' + 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']) - msg = 'Unable to parse string "apple" at array index 2' + msg = 'Unable to parse string "apple" at position 2' with tm.assertRaisesRegexp(ValueError, msg): to_numeric(s, errors='raise')