diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 83a9edfb239e2..c797114b17cc0 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..b22f249de2826 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -707,3 +707,21 @@ 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], 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])), + ], +) +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)