diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 33ce62415ea0f..9071d242e25b5 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -22,6 +22,7 @@ Bug fixes ~~~~~~~~~ - Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`) - Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`) +- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`) .. --------------------------------------------------------------------------- .. _whatsnew_201.other: diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py index f76fe166dba78..5859a6a8b556b 100644 --- a/pandas/core/arrays/arrow/array.py +++ b/pandas/core/arrays/arrow/array.py @@ -1044,6 +1044,11 @@ def to_numpy( result = np.empty(len(self), dtype=object) mask = ~self.isna() result[mask] = np.asarray(self[mask]._pa_array) + elif pa.types.is_null(self._pa_array.type): + result = np.asarray(self._pa_array, dtype=dtype) + if not isna(na_value): + result[:] = na_value + return result elif self._hasna: data = self.copy() data[self.isna()] = na_value diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 31d4c76a8db11..3ecbc723be2eb 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -27,6 +27,7 @@ import numpy as np import pytest +from pandas._libs import lib from pandas.compat import ( PY311, is_ci_environment, @@ -1676,6 +1677,23 @@ def test_to_numpy_int_with_na(): tm.assert_numpy_array_equal(result, expected) +@pytest.mark.parametrize("na_val, exp", [(lib.no_default, np.nan), (1, 1)]) +def test_to_numpy_null_array(na_val, exp): + # GH#52443 + arr = pd.array([pd.NA, pd.NA], dtype="null[pyarrow]") + result = arr.to_numpy(dtype="float64", na_value=na_val) + expected = np.array([exp] * 2, dtype="float64") + tm.assert_numpy_array_equal(result, expected) + + +def test_to_numpy_null_array_no_dtype(): + # GH#52443 + arr = pd.array([pd.NA, pd.NA], dtype="null[pyarrow]") + result = arr.to_numpy(dtype=None) + expected = np.array([pd.NA] * 2, dtype="object") + tm.assert_numpy_array_equal(result, expected) + + def test_setitem_null_slice(data): # GH50248 orig = data.copy()