Skip to content

Backport PR #57322 on branch 2.2.x (REGR: Fix astype conversion of ea int to td/dt with missing values) #57331

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

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
13 changes: 10 additions & 3 deletions pandas/core/arrays/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ def to_numpy_dtype_inference(
dtype = arr.dtype.numpy_dtype # type: ignore[union-attr]
elif dtype is not None:
dtype = np.dtype(dtype)
if na_value is lib.no_default and hasna and dtype.kind == "f":
na_value = np.nan
dtype_given = True
else:
dtype_given = True

if na_value is lib.no_default:
na_value = arr.dtype.na_value
if dtype is None or not hasna:
na_value = arr.dtype.na_value
elif dtype.kind == "f": # type: ignore[union-attr]
na_value = np.nan
elif dtype.kind == "M": # type: ignore[union-attr]
na_value = np.datetime64("nat")
elif dtype.kind == "m": # type: ignore[union-attr]
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 @@ -3305,6 +3305,15 @@ def test_arrow_floordiv_floating_0_divisor(dtype):
tm.assert_series_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)


@pytest.mark.parametrize("pa_type", tm.ALL_INT_PYARROW_DTYPES)
def test_arrow_integral_floordiv_large_values(pa_type):
# GH 56676
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_to_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas import (
NA,
Series,
Timedelta,
)
import pandas._testing as tm

Expand Down Expand Up @@ -34,3 +35,15 @@ def test_to_numpy_arrow_dtype_given():
result = ser.to_numpy(dtype="float64")
expected = np.array([1.0, np.nan])
tm.assert_numpy_array_equal(result, expected)


def test_astype_ea_int_to_td_ts():
# GH#57093
ser = Series([1, None], dtype="Int64")
result = ser.astype("m8[ns]")
expected = Series([1, Timedelta("nat")], dtype="m8[ns]")
tm.assert_series_equal(result, expected)

result = ser.astype("M8[ns]")
expected = Series([1, Timedelta("nat")], dtype="M8[ns]")
tm.assert_series_equal(result, expected)