Skip to content

Commit

Permalink
Backport PR pandas-dev#56644 on branch 2.2.x (BUG: Series.to_numpy ra…
Browse files Browse the repository at this point in the history
…ising for arrow floats to numpy floats) (pandas-dev#56648)

Backport PR pandas-dev#56644: BUG: Series.to_numpy raising for arrow floats to numpy floats

Co-authored-by: Patrick Hoefler <[email protected]>
  • Loading branch information
meeseeksmachine and phofl authored Dec 28, 2023
1 parent 95cc200 commit f8e9892
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CategoricalDtype,
is_array_like,
is_bool_dtype,
is_float_dtype,
is_integer,
is_list_like,
is_numeric_dtype,
Expand Down Expand Up @@ -1320,6 +1321,7 @@ def to_numpy(
copy: bool = False,
na_value: object = lib.no_default,
) -> np.ndarray:
original_na_value = na_value
dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, self._hasna)
pa_type = self._pa_array.type
if not self._hasna or isna(na_value) or pa.types.is_null(pa_type):
Expand All @@ -1345,7 +1347,14 @@ def to_numpy(
if dtype is not None and isna(na_value):
na_value = None
result = np.full(len(data), fill_value=na_value, dtype=dtype)
elif not data._hasna or (pa.types.is_floating(pa_type) and na_value is np.nan):
elif not data._hasna or (
pa.types.is_floating(pa_type)
and (
na_value is np.nan
or original_na_value is lib.no_default
and is_float_dtype(dtype)
)
):
result = data._pa_array.to_numpy()
if dtype is not None:
result = result.astype(dtype, copy=False)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,14 @@ def test_string_to_time_parsing_cast():
tm.assert_series_equal(result, expected)


def test_to_numpy_float():
# GH#56267
ser = pd.Series([32, 40, None], dtype="float[pyarrow]")
result = ser.astype("float64")
expected = pd.Series([32, 40, np.nan], dtype="float64")
tm.assert_series_equal(result, expected)


def test_to_numpy_timestamp_to_int():
# GH 55997
ser = pd.Series(["2020-01-01 04:30:00"], dtype="timestamp[ns][pyarrow]")
Expand Down

0 comments on commit f8e9892

Please sign in to comment.