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 all 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
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 @@ -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
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test the dtype=None, na_value=lib.no_default case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# 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