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

BUG: pd.to_numeric does not copy _mask for ExtensionArrays #39049

Merged
merged 2 commits into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def to_numeric(arg, errors="raise", downcast=None):
from pandas.core.arrays import FloatingArray, IntegerArray

klass = IntegerArray if is_integer_dtype(data.dtype) else FloatingArray
values = klass(data, mask)
values = klass(data, mask.copy())

if is_series:
return arg._constructor(values, index=arg.index, name=arg.name)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,16 @@ def test_downcast_nullable_numeric(data, input_dtype, downcast, expected_dtype):
result = pd.to_numeric(arr, downcast=downcast)
expected = pd.array(data, dtype=expected_dtype)
tm.assert_extension_array_equal(result, expected)


def test_downcast_nullable_mask_is_copied():
# GH38974

arr = pd.array([1, 2, pd.NA], dtype="Int64")

result = pd.to_numeric(arr, downcast="integer")
expected = pd.array([1, 2, pd.NA], dtype="Int8")
tm.assert_extension_array_equal(result, expected)

arr[1] = pd.NA # should not modify result
tm.assert_extension_array_equal(result, expected)