Skip to content

Commit c298a60

Browse files
authored
BUG: pd.to_numeric does not copy _mask for ExtensionArrays (#39049)
1 parent 55f2c44 commit c298a60

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def to_numeric(arg, errors="raise", downcast=None):
221221
from pandas.core.arrays import FloatingArray, IntegerArray
222222

223223
klass = IntegerArray if is_integer_dtype(data.dtype) else FloatingArray
224-
values = klass(data, mask)
224+
values = klass(data, mask.copy())
225225

226226
if is_series:
227227
return arg._constructor(values, index=arg.index, name=arg.name)

pandas/tests/tools/test_to_numeric.py

+13
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,16 @@ def test_downcast_nullable_numeric(data, input_dtype, downcast, expected_dtype):
764764
result = pd.to_numeric(arr, downcast=downcast)
765765
expected = pd.array(data, dtype=expected_dtype)
766766
tm.assert_extension_array_equal(result, expected)
767+
768+
769+
def test_downcast_nullable_mask_is_copied():
770+
# GH38974
771+
772+
arr = pd.array([1, 2, pd.NA], dtype="Int64")
773+
774+
result = pd.to_numeric(arr, downcast="integer")
775+
expected = pd.array([1, 2, pd.NA], dtype="Int8")
776+
tm.assert_extension_array_equal(result, expected)
777+
778+
arr[1] = pd.NA # should not modify result
779+
tm.assert_extension_array_equal(result, expected)

0 commit comments

Comments
 (0)