diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 752d08a526d8c..ba1eb35bc3a39 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1240,6 +1240,7 @@ Other - Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`) - Bug in :func:`eval` with ``engine="numexpr"`` returning unexpected result for float division. (:issue:`59736`) - Bug in :func:`to_numeric` raising ``TypeError`` when ``arg`` is a :class:`Timedelta` or :class:`Timestamp` scalar. (:issue:`59944`) +- Bug in :func:`to_numeric` with :class:`ArrowDtype` raising ``ValueError`` when the array contained NA values. (:issue:`61641`) - Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`) - Bug in :meth:`DataFrame.apply` raising ``RecursionError`` when passing ``func=list[int]``. (:issue:`61565`) - Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 14921457194ca..44b2948a6b362 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -222,8 +222,12 @@ def to_numeric( values_dtype = getattr(values, "dtype", None) if isinstance(values_dtype, ArrowDtype): + if is_numeric_dtype(values_dtype) and is_series: + return arg._constructor(values, index=arg.index, name=arg.name) + mask = values.isna() values = values.dropna().to_numpy() + new_mask: np.ndarray | None = None if is_numeric_dtype(values_dtype): pass diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index fcbc91d4c632f..9128a32a8af09 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -902,3 +902,20 @@ def test_coerce_pyarrow_backend(): result = to_numeric(ser, errors="coerce", dtype_backend="pyarrow") expected = Series([1, 2, None], dtype=ArrowDtype(pa.int64())) tm.assert_series_equal(result, expected) + + +@pytest.mark.parametrize( + "dtype", + [ + "ArrowDtype", + ], +) +def test_to_numeric_arrow_decimal_with_na(dtype): + # GH 61641 + pa = pytest.importorskip("pyarrow") + target_class = globals()[dtype] + decimal_type = target_class(pa.decimal128(3, scale=2)) + series = Series([1, None], dtype=decimal_type) + result = to_numeric(series, errors="coerce") + + tm.assert_series_equal(result, series)