Skip to content

Commit

Permalink
BUG: Don't segfault to_numeric when input is empty (pandas-dev#16305)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and jorisvandenbossche committed May 9, 2017
1 parent ce4eef3 commit 81aa70c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.20.2.txt
Expand Up @@ -37,7 +37,7 @@ Bug Fixes
Conversion
^^^^^^^^^^


- Bug in ``pd.to_numeric()`` in which empty data inputs were causing Python to crash (:issue:`16302`)


Indexing
Expand All @@ -49,7 +49,7 @@ Indexing
I/O
^^^

- Bug that would force importing of the clipboard routines unecessarily, potentially causing an import error on startup (:issue:`16288`)
- Bug that would force importing of the clipboard routines unnecessarily, potentially causing an import error on startup (:issue:`16288`)


Plotting
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/src/inference.pyx
Expand Up @@ -947,8 +947,13 @@ def maybe_convert_numeric(ndarray[object] values, set na_values,
-------
numeric_array : array of converted object values to numerical ones
"""

if len(values) == 0:
return np.array([], dtype='i8')

# fastpath for ints - try to convert all based on first value
cdef object val = values[0]

if util.is_integer_object(val):
try:
maybe_ints = values.astype('i8')
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/tools/test_numeric.py
Expand Up @@ -11,6 +11,21 @@

class TestToNumeric(object):

def test_empty(self):
# see gh-16302
s = pd.Series([], dtype=object)

res = to_numeric(s)
expected = pd.Series([], dtype=np.int64)

tm.assert_series_equal(res, expected)

# Original issue example
res = to_numeric(s, errors='coerce', downcast='integer')
expected = pd.Series([], dtype=np.int8)

tm.assert_series_equal(res, expected)

def test_series(self):
s = pd.Series(['1', '-3.14', '7'])
res = to_numeric(s)
Expand Down

0 comments on commit 81aa70c

Please sign in to comment.