Skip to content

Commit 7974ad0

Browse files
authored
BUG: segfault for null dtype in to_numpy (#52466)
1 parent 4d64a8a commit 7974ad0

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Bug fixes
2222
~~~~~~~~~
2323
- Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`)
2424
- Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`)
25+
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)
2526

2627
.. ---------------------------------------------------------------------------
2728
.. _whatsnew_201.other:

pandas/core/arrays/arrow/array.py

+5
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,11 @@ def to_numpy(
10441044
result = np.empty(len(self), dtype=object)
10451045
mask = ~self.isna()
10461046
result[mask] = np.asarray(self[mask]._pa_array)
1047+
elif pa.types.is_null(self._pa_array.type):
1048+
result = np.asarray(self._pa_array, dtype=dtype)
1049+
if not isna(na_value):
1050+
result[:] = na_value
1051+
return result
10471052
elif self._hasna:
10481053
data = self.copy()
10491054
data[self.isna()] = na_value

pandas/tests/extension/test_arrow.py

+18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import numpy as np
2828
import pytest
2929

30+
from pandas._libs import lib
3031
from pandas.compat import (
3132
PY311,
3233
is_ci_environment,
@@ -1676,6 +1677,23 @@ def test_to_numpy_int_with_na():
16761677
tm.assert_numpy_array_equal(result, expected)
16771678

16781679

1680+
@pytest.mark.parametrize("na_val, exp", [(lib.no_default, np.nan), (1, 1)])
1681+
def test_to_numpy_null_array(na_val, exp):
1682+
# GH#52443
1683+
arr = pd.array([pd.NA, pd.NA], dtype="null[pyarrow]")
1684+
result = arr.to_numpy(dtype="float64", na_value=na_val)
1685+
expected = np.array([exp] * 2, dtype="float64")
1686+
tm.assert_numpy_array_equal(result, expected)
1687+
1688+
1689+
def test_to_numpy_null_array_no_dtype():
1690+
# GH#52443
1691+
arr = pd.array([pd.NA, pd.NA], dtype="null[pyarrow]")
1692+
result = arr.to_numpy(dtype=None)
1693+
expected = np.array([pd.NA] * 2, dtype="object")
1694+
tm.assert_numpy_array_equal(result, expected)
1695+
1696+
16791697
def test_setitem_null_slice(data):
16801698
# GH50248
16811699
orig = data.copy()

0 commit comments

Comments
 (0)