Skip to content

Commit 84423d8

Browse files
phoflmroeschke
andauthored
Backport PR #52466 on branch 2.0.x (BUG: segfault for null dtype in to_numpy) (#52497)
* BUG: segfault for null dtype in to_numpy (#52466) (cherry picked from commit 7974ad0) * Update pandas/core/arrays/arrow/array.py * Update pandas/core/arrays/arrow/array.py --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 00a825f commit 84423d8

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
@@ -1024,6 +1024,11 @@ def to_numpy(
10241024
result = np.empty(len(self), dtype=object)
10251025
mask = ~self.isna()
10261026
result[mask] = np.asarray(self[mask]._data)
1027+
elif pa.types.is_null(self._data.type):
1028+
result = np.asarray(self._data, dtype=dtype)
1029+
if not isna(na_value):
1030+
result[:] = na_value
1031+
return result
10271032
elif self._hasna:
10281033
data = self.copy()
10291034
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,
@@ -1675,6 +1676,23 @@ def test_to_numpy_int_with_na():
16751676
tm.assert_numpy_array_equal(result, expected)
16761677

16771678

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

0 commit comments

Comments
 (0)