Skip to content

BUG: to_numeric casting to ea for new string dtype #56179

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

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bug fixes
~~~~~~~~~
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
- Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
- Fixed bug in :meth:`Index.insert` casting object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)
- Fixed bug in :meth:`Series.str.translate` losing object dtype when string option is set (:issue:`56152`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def to_numeric(
set(),
coerce_numeric=coerce_numeric,
convert_to_masked_nullable=dtype_backend is not lib.no_default
or isinstance(values_dtype, StringDtype),
or isinstance(values_dtype, StringDtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an issue for the pyarrow storage either?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we want nullable dtype in this case, the mapping is as follows:

  • storage=pyarrow_numpy -> NumPy backed
  • storage=payrrow -> Nullable
  • ArrowDtype string -> PyArrow backed numerics

and not values_dtype.storage == "pyarrow_numpy",
)
except (ValueError, TypeError):
if errors == "raise":
Expand All @@ -249,6 +250,7 @@ def to_numeric(
dtype_backend is not lib.no_default
and new_mask is None
or isinstance(values_dtype, StringDtype)
and not values_dtype.storage == "pyarrow_numpy"
):
new_mask = np.zeros(values.shape, dtype=np.bool_)

Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from numpy import iinfo
import pytest

import pandas.util._test_decorators as td

import pandas as pd
from pandas import (
ArrowDtype,
DataFrame,
Index,
Series,
option_context,
to_numeric,
)
import pandas._testing as tm
Expand Down Expand Up @@ -67,10 +70,14 @@ def test_empty(input_kwargs, result_kwargs):
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
)
@pytest.mark.parametrize("last_val", ["7", 7])
def test_series(last_val):
ser = Series(["1", "-3.14", last_val])
result = to_numeric(ser)
def test_series(last_val, infer_string):
with option_context("future.infer_string", infer_string):
ser = Series(["1", "-3.14", last_val])
result = to_numeric(ser)

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