Skip to content

REGR: Fix to_numpy for masked array with non-numeric dtype #57121

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 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ def to_numpy(
"""
hasna = self._hasna
dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, hasna)
if dtype is None:
dtype = object

if hasna:
if (
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/masked/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays import BaseMaskedArray

arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
arrays += [
Expand Down Expand Up @@ -55,3 +56,19 @@ def test_tolist(data):
result = data.tolist()
expected = list(data)
tm.assert_equal(result, expected)


def test_to_numpy():
# GH#56991

class MyStringArray(BaseMaskedArray):
dtype = pd.StringDtype()
_dtype_cls = pd.StringDtype
_internal_fill_value = pd.NA

arr = MyStringArray(
values=np.array(["a", "b", "c"]), mask=np.array([False, True, False])
)
result = arr.to_numpy()
expected = np.array(["a", pd.NA, "c"])
tm.assert_numpy_array_equal(result, expected)