Skip to content
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

ENH: support downcasting of nullable EAs in pd.to_numeric #38746

Merged
merged 20 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 6 additions & 7 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,17 @@ def to_numeric(arg, errors="raise", downcast=None):
break

if mask is not None:
buf = np.zeros(mask.shape, dtype=values.dtype)
buf[~mask] = values
values = buf
data = np.zeros(mask.shape, dtype=values.dtype)
data[~mask] = values

if is_integer_dtype(values):
if is_integer_dtype(data):
from pandas.core.arrays import IntegerArray

values = IntegerArray(values, mask)
elif is_float_dtype(values):
values = IntegerArray(data, mask)
elif is_float_dtype(data):
from pandas.core.arrays import FloatingArray

values = FloatingArray(values, mask)
values = FloatingArray(data, mask)

if is_series:
return arg._constructor(values, index=arg.index, name=arg.name)
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,17 +731,23 @@ def test_to_numeric_from_nullable_string(values, expected):
"data, input_dtype, downcast, expected_dtype",
(
([1, 1], "Int64", "integer", "Int8"),
([1.0, 1.0], "Float64", "integer", "Int8"),
arw2019 marked this conversation as resolved.
Show resolved Hide resolved
([1.0, 1.1], "Float64", "integer", "Float64"),
([1, pd.NA], "Int64", "integer", "Int8"),
([450, 300], "Int64", "integer", "Int16"),
([1, 1], "Int64", "signed", "Int8"),
([1.0, 1.0], "Float32", "signed", "Int8"),
([1.0, 1.1], "Float64", "signed", "Float64"),
([1, pd.NA], "Int64", "signed", "Int8"),
jreback marked this conversation as resolved.
Show resolved Hide resolved
([450, -300], "Int64", "signed", "Int16"),
([1, 1], "Int64", "unsigned", "UInt8"),
([1.0, 1.0], "Float32", "unsigned", "UInt8"),
([1.0, 1.1], "Float64", "unsigned", "Float64"),
([1, pd.NA], "Int64", "unsigned", "UInt8"),
([450, -300], "Int64", "unsigned", "Int64"),
([-1, -1], "Int32", "unsigned", "Int32"),
([1, 1], "Float64", "float", "Float32"),
([1, 1], "Float64", "float", "Float32"),
([1, 1.1], "Float64", "float", "Float32"),
([1, 1], "Float64", "integer", "Int8"),
arw2019 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Nit: maybe move this one up to the other "integer" downcast cases

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

),
)
Expand Down