From 048979f56eeaa15959e49dc3431041de41ef1a57 Mon Sep 17 00:00:00 2001 From: Chilin Chiou Date: Sun, 15 Jun 2025 02:16:30 +0800 Subject: [PATCH 1/3] BUG: to_numeric raise ValueError when the arrow array contains NA --- pandas/core/tools/numeric.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index bc45343d6e2d3..fe76c125ba77f 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -211,8 +211,15 @@ def to_numeric( values_dtype = getattr(values, "dtype", None) if isinstance(values_dtype, ArrowDtype): + if is_numeric_dtype(values_dtype): + if is_series: + return arg._constructor(values, index=arg.index, name=arg.name) + else: + return values + mask = values.isna() values = values.dropna().to_numpy() + new_mask: np.ndarray | None = None if is_numeric_dtype(values_dtype): pass From f543e5dbdc44428ca5c8c1618cdc373b86d66dfb Mon Sep 17 00:00:00 2001 From: Chilin Chiou Date: Sun, 15 Jun 2025 02:18:41 +0800 Subject: [PATCH 2/3] TST: to_numeric raise ValueError when the arrow array contains NA --- pandas/tests/tools/test_to_numeric.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 893f526fb3eb0..94e3867e46362 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -919,3 +919,14 @@ 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) + + +def test_to_numeric_arrow_decimal_with_na(): + # GH 61641 + pa = pytest.importorskip("pyarrow") + decimal_type = ArrowDtype(pa.decimal128(3, scale=2)) + series = Series([1, None], dtype=decimal_type) + result = to_numeric(series, errors="coerce") + + expected = Series([1.00, pd.NA], dtype=decimal_type) + tm.assert_series_equal(result, expected) From 939d6a6b4a63f06e1ce220daeae83ffd426ec807 Mon Sep 17 00:00:00 2001 From: Chilin Chiou Date: Sun, 15 Jun 2025 02:19:08 +0800 Subject: [PATCH 3/3] add changelog entry --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 5160d2ea8b8fe..caa352d006c2b 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -884,6 +884,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` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`) - Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`)