From dba28b6cb84753f3b4a912f741960be864fc4422 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 17 Dec 2022 23:49:12 +0100 Subject: [PATCH] BUG: to_numpy for PandasArray does not handle na_value --- doc/source/whatsnew/v2.0.0.rst | 2 +- pandas/core/arrays/numpy_.py | 14 +++++++++----- pandas/tests/arrays/test_array.py | 8 ++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index bc5d66e8e7955..d390a871e5f0d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -931,7 +931,7 @@ ExtensionArray - Bug in :meth:`Series.mean` overflowing unnecessarily with nullable integers (:issue:`48378`) - Bug in :meth:`Series.tolist` for nullable dtypes returning numpy scalars instead of python scalars (:issue:`49890`) - Bug when concatenating an empty DataFrame with an ExtensionDtype to another DataFrame with the same ExtensionDtype, the resulting dtype turned into object (:issue:`48510`) -- +- Bug in :meth:`array.PandasArray.to_numpy` raising with ``NA`` value when ``na_value`` is specified (:issue:`40638`) Styler ^^^^^^ diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 48679a8355837..2ed42d699e862 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -389,13 +389,17 @@ def to_numpy( copy: bool = False, na_value: object = lib.no_default, ) -> np.ndarray: - result = np.asarray(self._ndarray, dtype=dtype) + mask = self.isna() + if na_value is not lib.no_default and mask.any(): + result = self._ndarray.copy() + result[mask] = na_value + else: + result = self._ndarray - if (copy or na_value is not lib.no_default) and result is self._ndarray: - result = result.copy() + result = np.asarray(result, dtype=dtype) - if na_value is not lib.no_default: - result[self.isna()] = na_value + if copy and result is self._ndarray: + result = result.copy() return result diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 14ca7a61c50f5..2288ac408d99e 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -413,3 +413,11 @@ def test_array_not_registered(registry_without_decimal): result = pd.array(data, dtype=DecimalDtype) expected = DecimalArray._from_sequence(data) tm.assert_equal(result, expected) + + +def test_array_to_numpy_na(): + # GH#40638 + arr = pd.array([pd.NA, 1], dtype="string") + result = arr.to_numpy(na_value=True, dtype=bool) + expected = np.array([True, True]) + tm.assert_numpy_array_equal(result, expected)