Skip to content

Commit d47ad02

Browse files
authored
BUG: to_numeric casting to ea for new string dtype (#56179)
1 parent 59a1758 commit d47ad02

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

doc/source/whatsnew/v2.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bug fixes
2323
~~~~~~~~~
2424
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
2525
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
26+
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
2627
- Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
2728
- Fixed bug in :meth:`DataFrame.to_hdf` raising when columns have ``StringDtype`` (:issue:`55088`)
2829
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)

pandas/core/tools/numeric.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ def to_numeric(
234234
set(),
235235
coerce_numeric=coerce_numeric,
236236
convert_to_masked_nullable=dtype_backend is not lib.no_default
237-
or isinstance(values_dtype, StringDtype),
237+
or isinstance(values_dtype, StringDtype)
238+
and not values_dtype.storage == "pyarrow_numpy",
238239
)
239240
except (ValueError, TypeError):
240241
if errors == "raise":
@@ -249,6 +250,7 @@ def to_numeric(
249250
dtype_backend is not lib.no_default
250251
and new_mask is None
251252
or isinstance(values_dtype, StringDtype)
253+
and not values_dtype.storage == "pyarrow_numpy"
252254
):
253255
new_mask = np.zeros(values.shape, dtype=np.bool_)
254256

pandas/tests/tools/test_to_numeric.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
from numpy import iinfo
55
import pytest
66

7+
import pandas.util._test_decorators as td
8+
79
import pandas as pd
810
from pandas import (
911
ArrowDtype,
1012
DataFrame,
1113
Index,
1214
Series,
15+
option_context,
1316
to_numeric,
1417
)
1518
import pandas._testing as tm
@@ -67,10 +70,14 @@ def test_empty(input_kwargs, result_kwargs):
6770
tm.assert_series_equal(result, expected)
6871

6972

73+
@pytest.mark.parametrize(
74+
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
75+
)
7076
@pytest.mark.parametrize("last_val", ["7", 7])
71-
def test_series(last_val):
72-
ser = Series(["1", "-3.14", last_val])
73-
result = to_numeric(ser)
77+
def test_series(last_val, infer_string):
78+
with option_context("future.infer_string", infer_string):
79+
ser = Series(["1", "-3.14", last_val])
80+
result = to_numeric(ser)
7481

7582
expected = Series([1, -3.14, 7])
7683
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)