Skip to content

Commit

Permalink
Backport PR pandas-dev#57322: REGR: Fix astype conversion of ea int t…
Browse files Browse the repository at this point in the history
…o td/dt with missing values
  • Loading branch information
phofl authored and meeseeksmachine committed Feb 10, 2024
1 parent 361b089 commit aaceb35
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
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)

0 comments on commit aaceb35

Please sign in to comment.