Skip to content

Commit 471c852

Browse files
authored
BUG: to_numeric(errors='coerce', dtype_backend='pyarrow') with ArrowDtype (#52606)
* BUG: to_numeric(errors='coerce', dtype_backend='pyarrow') with ArrowDtype * Use safer check * Remove unnecessary check
1 parent ebf6c8f commit 471c852

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
@@ -275,7 +275,8 @@ def to_numeric(
275275
# GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct
276276
# masked array
277277
if (mask is not None or new_mask is not None) and not is_string_dtype(values.dtype):
278-
if mask is None:
278+
if mask is None or (new_mask is not None and new_mask.shape == mask.shape):
279+
# GH 52588
279280
mask = new_mask
280281
else:
281282
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)