Skip to content

Commit 62dbbe6

Browse files
authored
BUG: Series.to_numpy raising for arrow floats to numpy floats (#56644)
* BUG: Series.to_numpy raising for arrow floats to numpy floats * Fixup
1 parent 8872b9e commit 62dbbe6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pandas/core/arrays/arrow/array.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
CategoricalDtype,
3838
is_array_like,
3939
is_bool_dtype,
40+
is_float_dtype,
4041
is_integer,
4142
is_list_like,
4243
is_numeric_dtype,
@@ -1320,6 +1321,7 @@ def to_numpy(
13201321
copy: bool = False,
13211322
na_value: object = lib.no_default,
13221323
) -> np.ndarray:
1324+
original_na_value = na_value
13231325
dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, self._hasna)
13241326
pa_type = self._pa_array.type
13251327
if not self._hasna or isna(na_value) or pa.types.is_null(pa_type):
@@ -1345,7 +1347,14 @@ def to_numpy(
13451347
if dtype is not None and isna(na_value):
13461348
na_value = None
13471349
result = np.full(len(data), fill_value=na_value, dtype=dtype)
1348-
elif not data._hasna or (pa.types.is_floating(pa_type) and na_value is np.nan):
1350+
elif not data._hasna or (
1351+
pa.types.is_floating(pa_type)
1352+
and (
1353+
na_value is np.nan
1354+
or original_na_value is lib.no_default
1355+
and is_float_dtype(dtype)
1356+
)
1357+
):
13491358
result = data._pa_array.to_numpy()
13501359
if dtype is not None:
13511360
result = result.astype(dtype, copy=False)

pandas/tests/extension/test_arrow.py

+8
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,14 @@ def test_string_to_time_parsing_cast():
31533153
tm.assert_series_equal(result, expected)
31543154

31553155

3156+
def test_to_numpy_float():
3157+
# GH#56267
3158+
ser = pd.Series([32, 40, None], dtype="float[pyarrow]")
3159+
result = ser.astype("float64")
3160+
expected = pd.Series([32, 40, np.nan], dtype="float64")
3161+
tm.assert_series_equal(result, expected)
3162+
3163+
31563164
def test_to_numpy_timestamp_to_int():
31573165
# GH 55997
31583166
ser = pd.Series(["2020-01-01 04:30:00"], dtype="timestamp[ns][pyarrow]")

0 commit comments

Comments
 (0)