Skip to content

BUG: to_numeric fails to convert a Pyarrow Decimal series containing NA values #61659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading