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