From efe2c00c32c5e2b0f747ff3533bcf405a43f53fc Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 19 Oct 2020 19:22:12 -0500 Subject: [PATCH 1/3] BUG: Don't raise TypeError when converting NA from string to numeric --- doc/source/whatsnew/v1.2.0.rst | 2 +- pandas/_libs/lib.pyx | 2 +- pandas/tests/tools/test_to_numeric.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index fd6e7c8e9cc02..040ae8c21f91d 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -409,7 +409,7 @@ Conversion Strings ^^^^^^^ - Bug in :meth:`Series.to_string`, :meth:`DataFrame.to_string`, and :meth:`DataFrame.to_latex` adding a leading space when ``index=False`` (:issue:`24980`) -- +- Bug in :func:`to_numeric` raising a ``TypeError`` when attempting to convert a string dtype :class:`Series` containing only numeric strings and ``NA`` (:issue:`37262`) - diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index f4caafb3a9fe7..001fbae120ae8 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2019,7 +2019,7 @@ def maybe_convert_numeric(ndarray[object] values, set na_values, elif util.is_bool_object(val): floats[i] = uints[i] = ints[i] = bools[i] = val seen.bool_ = True - elif val is None: + elif val is None or val is C_NA: seen.saw_null() floats[i] = complexes[i] = NaN elif hasattr(val, '__len__') and len(val) == 0: diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 450076f2824ad..f69d7feb2a074 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -707,3 +707,17 @@ def test_precision_float_conversion(strrep): result = to_numeric(strrep) assert result == float(strrep) + + +@pytest.mark.parametrize( + "values, expected", + [ + (["1", "2", None], Series([1, 2, np.nan])), + (["1", "2", "3"], Series([1, 2, 3])), + (["1", "2", "3.5"], Series([1, 2, 3.5])), + ], +) +def test_to_numeric_from_nullable_string(values, expected): + s = Series(values, dtype="string") + result = to_numeric(s) + tm.assert_series_equal(result, expected) From ca371cf5cb533ea9cdc41827a9954aabea522c01 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Mon, 19 Oct 2020 19:38:34 -0500 Subject: [PATCH 2/3] Link --- pandas/tests/tools/test_to_numeric.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index f69d7feb2a074..c3936165957e3 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -718,6 +718,7 @@ def test_precision_float_conversion(strrep): ], ) def test_to_numeric_from_nullable_string(values, expected): + # https://github.com/pandas-dev/pandas/issues/37262 s = Series(values, dtype="string") result = to_numeric(s) tm.assert_series_equal(result, expected) From 039f714aa2380fa780436105581e1c40fedbe50a Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 20 Oct 2020 12:18:40 -0500 Subject: [PATCH 3/3] Tests --- pandas/tests/tools/test_to_numeric.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index c3936165957e3..b22f249de2826 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -714,6 +714,9 @@ def test_precision_float_conversion(strrep): [ (["1", "2", None], Series([1, 2, np.nan])), (["1", "2", "3"], Series([1, 2, 3])), + (["1", "2", 3], Series([1, 2, 3])), + (["1", "2", 3.5], Series([1, 2, 3.5])), + (["1", None, 3.5], Series([1, np.nan, 3.5])), (["1", "2", "3.5"], Series([1, 2, 3.5])), ], )