Skip to content

Commit

Permalink
Backport PR #52466 on branch 2.0.x (BUG: segfault for null dtype in t…
Browse files Browse the repository at this point in the history
…o_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 <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
phofl and mroeschke committed Apr 7, 2023
1 parent 00a825f commit 84423d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,11 @@ def to_numpy(
result = np.empty(len(self), dtype=object)
mask = ~self.isna()
result[mask] = np.asarray(self[mask]._data)
elif pa.types.is_null(self._data.type):
result = np.asarray(self._data, dtype=dtype)
if not isna(na_value):
result[:] = na_value
return result
elif self._hasna:
data = self.copy()
data[self.isna()] = na_value
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import numpy as np
import pytest

from pandas._libs import lib
from pandas.compat import (
PY311,
is_ci_environment,
Expand Down Expand Up @@ -1675,6 +1676,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()
Expand Down

0 comments on commit 84423d8

Please sign in to comment.