Skip to content

Commit 2e6c99a

Browse files
Backport PR #52606 on branch 2.0.x (BUG: to_numeric(errors='coerce', dtype_backend='pyarrow') with ArrowDtype) (#52611)
Backport PR #52606: BUG: to_numeric(errors='coerce', dtype_backend='pyarrow') with ArrowDtype Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7e6261a commit 2e6c99a

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bug fixes
3131
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
3232
- Bug in :func:`read_csv` casting PyArrow datetimes to NumPy when ``dtype_backend="pyarrow"`` and ``parse_dates`` is set causing a performance bottleneck in the process (:issue:`52546`)
3333
- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`)
34+
- Bug in :func:`to_numeric` with ``errors='coerce'`` and ``dtype_backend='pyarrow'`` with :class:`ArrowDtype` data (:issue:`52588`)
3435

3536
.. ---------------------------------------------------------------------------
3637
.. _whatsnew_201.other:

pandas/core/tools/numeric.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ def to_numeric(
270270
# GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct
271271
# masked array
272272
if (mask is not None or new_mask is not None) and not is_string_dtype(values.dtype):
273-
if mask is None:
273+
if mask is None or (new_mask is not None and new_mask.shape == mask.shape):
274+
# GH 52588
274275
mask = new_mask
275276
else:
276277
mask = mask.copy()

pandas/tests/tools/test_to_numeric.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pandas as pd
88
from pandas import (
9+
ArrowDtype,
910
DataFrame,
1011
Index,
1112
Series,
@@ -942,3 +943,12 @@ def test_invalid_dtype_backend():
942943
)
943944
with pytest.raises(ValueError, match=msg):
944945
to_numeric(ser, dtype_backend="numpy")
946+
947+
948+
def test_coerce_pyarrow_backend():
949+
# GH 52588
950+
pa = pytest.importorskip("pyarrow")
951+
ser = Series(list("12x"), dtype=ArrowDtype(pa.string()))
952+
result = to_numeric(ser, errors="coerce", dtype_backend="pyarrow")
953+
expected = Series([1, 2, None], dtype=ArrowDtype(pa.int64()))
954+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)