Skip to content

Commit b00689e

Browse files
phoflpmhatre1
authored andcommitted
REGR: Fix to_numpy for masked array with non-numeric dtype (pandas-dev#57121)
* REGR: Fix to_numpy for masked array with non-numeric dtype * Add whatsnew * Update docstring
1 parent b7caa1b commit b00689e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v2.2.1.rst

+3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ Fixed regressions
1717
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
1818
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
1919
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
20+
- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
21+
- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
2022
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
2123
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
2224
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
2325
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
2426
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
2527
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
28+
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
2629
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
2730
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
2831
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)

pandas/core/arrays/masked.py

+2
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ def to_numpy(
502502
"""
503503
hasna = self._hasna
504504
dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, hasna)
505+
if dtype is None:
506+
dtype = object
505507

506508
if hasna:
507509
if (

pandas/tests/arrays/masked/test_function.py

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pandas as pd
77
import pandas._testing as tm
8+
from pandas.core.arrays import BaseMaskedArray
89

910
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
1011
arrays += [
@@ -55,3 +56,19 @@ def test_tolist(data):
5556
result = data.tolist()
5657
expected = list(data)
5758
tm.assert_equal(result, expected)
59+
60+
61+
def test_to_numpy():
62+
# GH#56991
63+
64+
class MyStringArray(BaseMaskedArray):
65+
dtype = pd.StringDtype()
66+
_dtype_cls = pd.StringDtype
67+
_internal_fill_value = pd.NA
68+
69+
arr = MyStringArray(
70+
values=np.array(["a", "b", "c"]), mask=np.array([False, True, False])
71+
)
72+
result = arr.to_numpy()
73+
expected = np.array(["a", pd.NA, "c"])
74+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)