Skip to content

Commit 8f64ad7

Browse files
chris-b1jorisvandenbossche
authored andcommitted
DOC: show failing string on numeric parse (#13773)
1 parent 98c5b88 commit 8f64ad7

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

pandas/src/inference.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,9 @@ def maybe_convert_numeric(object[:] values, set na_values,
686686
raise ValueError('integer out of range')
687687
else:
688688
seen_float = True
689-
except:
689+
except (TypeError, ValueError) as e:
690690
if not coerce_numeric:
691-
raise
691+
raise type(e)(str(e) + ' at position {}'.format(i))
692692

693693
floats[i] = nan
694694
seen_float = True

pandas/src/parse_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int floatify(PyObject* str, double *result, int *maybe_int) {
6666
return 0;
6767

6868
parsingerror:
69-
PyErr_SetString(PyExc_ValueError, "Unable to parse string");
69+
PyErr_Format(PyExc_ValueError, "Unable to parse string \"%s\"", data);
7070
Py_XDECREF(tmp);
7171
return -1;
7272

pandas/tools/tests/test_util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def test_series_numeric(self):
120120

121121
def test_error(self):
122122
s = pd.Series([1, -3.14, 'apple'])
123-
with tm.assertRaises(ValueError):
123+
msg = 'Unable to parse string "apple" at position 2'
124+
with tm.assertRaisesRegexp(ValueError, msg):
124125
to_numeric(s, errors='raise')
125126

126127
res = to_numeric(s, errors='ignore')
@@ -131,9 +132,15 @@ def test_error(self):
131132
expected = pd.Series([1, -3.14, np.nan])
132133
tm.assert_series_equal(res, expected)
133134

135+
s = pd.Series(['orange', 1, -3.14, 'apple'])
136+
msg = 'Unable to parse string "orange" at position 0'
137+
with tm.assertRaisesRegexp(ValueError, msg):
138+
to_numeric(s, errors='raise')
139+
134140
def test_error_seen_bool(self):
135141
s = pd.Series([True, False, 'apple'])
136-
with tm.assertRaises(ValueError):
142+
msg = 'Unable to parse string "apple" at position 2'
143+
with tm.assertRaisesRegexp(ValueError, msg):
137144
to_numeric(s, errors='raise')
138145

139146
res = to_numeric(s, errors='ignore')

0 commit comments

Comments
 (0)