Skip to content

REGR: Series.astype raising TypeError when converting a pyarrow integer type with nulls to numpy float #57170

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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Fixed regressions
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`, :issue:`57093`)

.. ---------------------------------------------------------------------------
.. _whatsnew_221.bug_fixes:
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/arrays/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ def to_numpy_dtype_inference(
dtype_given = True

if na_value is lib.no_default:
na_value = arr.dtype.na_value
if dtype is None:
na_value = arr.dtype.na_value
elif dtype.kind == "f":
na_value = np.nan
elif dtype.kind == "M":
na_value = np.datetime64("nat")
elif dtype.kind == "m":
na_value = np.timedelta64("nat")
else:
na_value = arr.dtype.na_value

if not dtype_given and hasna:
try:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3480,3 +3480,12 @@ def test_to_numpy_timestamp_to_int():
result = ser.to_numpy(dtype=np.int64)
expected = np.array([1577853000000000000])
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("dtype", ["float64", "datetime64[ns]", "timedelta64[ns]"])
def test_astype_int_with_null_to_numpy_dtype(dtype):
# GH 57093
ser = pd.Series([1, None], dtype="int64[pyarrow]")
result = ser.astype(dtype)
expected = pd.Series([1, None], dtype=dtype)
tm.assert_series_equal(result, expected)