Skip to content

Commit

Permalink
BUG: pd.to_numeric does not copy _mask for ExtensionArrays (pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
arw2019 authored and luckyvs1 committed Jan 20, 2021
1 parent 632f1c4 commit 84bfabd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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)

0 comments on commit 84bfabd

Please sign in to comment.