Skip to content
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

Backport PR #56644 on branch 2.2.x (BUG: Series.to_numpy raising for arrow floats to numpy floats) #56648

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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