Skip to content

Commit 11a6136

Browse files
Backport PR pandas-dev#57233 on branch 2.2.x (REGR: Fix to_numpy conversion for arrow ea with float dtype given) (pandas-dev#57294)
Backport PR pandas-dev#57233: REGR: Fix to_numpy conversion for arrow ea with float dtype given Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 45fc954 commit 11a6136

File tree

6 files changed

+19
-7
lines changed

6 files changed

+19
-7
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
- 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`)
2626
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
2727
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
28+
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
2829

2930
.. ---------------------------------------------------------------------------
3031
.. _whatsnew_221.bug_fixes:

pandas/core/arrays/_utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def to_numpy_dtype_inference(
3939
dtype = arr.dtype.numpy_dtype # type: ignore[union-attr]
4040
elif dtype is not None:
4141
dtype = np.dtype(dtype)
42+
if na_value is lib.no_default and hasna and dtype.kind == "f":
43+
na_value = np.nan
4244
dtype_given = True
4345
else:
4446
dtype_given = True

pandas/tests/arrays/boolean/test_construction.py

-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,6 @@ def test_to_numpy(box):
308308
# converting to int or float without specifying na_value raises
309309
with pytest.raises(ValueError, match="cannot convert to 'int64'-dtype"):
310310
arr.to_numpy(dtype="int64")
311-
with pytest.raises(ValueError, match="cannot convert to 'float64'-dtype"):
312-
arr.to_numpy(dtype="float64")
313311

314312

315313
def test_to_numpy_copy():

pandas/tests/arrays/floating/test_to_numpy.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def test_to_numpy_float(box):
3333
tm.assert_numpy_array_equal(result, expected)
3434

3535
arr = con([0.1, 0.2, None], dtype="Float64")
36-
with pytest.raises(ValueError, match="cannot convert to 'float64'-dtype"):
37-
result = arr.to_numpy(dtype="float64")
36+
result = arr.to_numpy(dtype="float64")
37+
expected = np.array([0.1, 0.2, np.nan], dtype="float64")
38+
tm.assert_numpy_array_equal(result, expected)
3839

39-
# need to explicitly specify na_value
4040
result = arr.to_numpy(dtype="float64", na_value=np.nan)
4141
expected = np.array([0.1, 0.2, np.nan], dtype="float64")
4242
tm.assert_numpy_array_equal(result, expected)
@@ -100,7 +100,7 @@ def test_to_numpy_dtype(box, dtype):
100100
tm.assert_numpy_array_equal(result, expected)
101101

102102

103-
@pytest.mark.parametrize("dtype", ["float64", "float32", "int32", "int64", "bool"])
103+
@pytest.mark.parametrize("dtype", ["int32", "int64", "bool"])
104104
@pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
105105
def test_to_numpy_na_raises(box, dtype):
106106
con = pd.Series if box else pd.array

pandas/tests/arrays/integer/test_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def test_to_numpy_dtype(dtype, in_series):
271271
tm.assert_numpy_array_equal(result, expected)
272272

273273

274-
@pytest.mark.parametrize("dtype", ["float64", "int64", "bool"])
274+
@pytest.mark.parametrize("dtype", ["int64", "bool"])
275275
def test_to_numpy_na_raises(dtype):
276276
a = pd.array([0, 1, None], dtype="Int64")
277277
with pytest.raises(ValueError, match=dtype):

pandas/tests/series/methods/test_to_numpy.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import numpy as np
22
import pytest
33

4+
import pandas.util._test_decorators as td
5+
46
from pandas import (
57
NA,
68
Series,
@@ -23,3 +25,12 @@ def test_to_numpy_cast_before_setting_na():
2325
result = ser.to_numpy(dtype=np.float64, na_value=np.nan)
2426
expected = np.array([1.0])
2527
tm.assert_numpy_array_equal(result, expected)
28+
29+
30+
@td.skip_if_no("pyarrow")
31+
def test_to_numpy_arrow_dtype_given():
32+
# GH#57121
33+
ser = Series([1, NA], dtype="int64[pyarrow]")
34+
result = ser.to_numpy(dtype="float64")
35+
expected = np.array([1.0, np.nan])
36+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)