Skip to content

BUG: segfault for null dtype in to_numpy #52466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1634,7 +1639,9 @@ def _replace_with_mask(
indices = pa.array(indices, type=pa.int64())
replacements = replacements.take(indices)
return cls._if_else(mask, replacements, values)
if isinstance(values, pa.ChunkedArray) and pa.types.is_boolean(values.type):
if isinstance(values, pa.ChunkedArray) and (
pa.types.is_boolean(values.type) or pa.types.is_null(values.type)
):
# GH#52059 replace_with_mask segfaults for chunked array
# https://github.com/apache/arrow/issues/34634
values = values.combine_chunks()
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 @@ -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, dtype):
# GH#52443
arr = pd.array([pd.NA, pd.NA], dtype="null[pyarrow]")
result = arr.to_numpy(dtype=dtype, 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