Skip to content

DOC: show failing string on numeric parse #13773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/src/parse_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 9 additions & 2 deletions pandas/tools/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down