|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pandas.testing as tm |
| 4 | +import pytest |
| 5 | + |
| 6 | +def test_series_astype_nullable_int_preserves_nans(): |
| 7 | + # regression/edge: astype -> nullable integer dtype should preserve NaNs |
| 8 | + s = pd.Series([1, np.nan, 3], dtype="float64") |
| 9 | + res = s.astype("Int64") |
| 10 | + # expected: dtype is nullable Int64 and NaN is represented as <NA> |
| 11 | + assert res.dtype == "Int64" |
| 12 | + expected = pd.Series([1, pd.NA, 3], dtype="Int64") |
| 13 | + tm.assert_series_equal(res, expected) |
| 14 | + |
| 15 | +def test_series_astype_from_nullable_int_to_float_roundtrip(): |
| 16 | + # convert nullable Int64 -> float -> Int64, ensure values and missingness preserved |
| 17 | + s = pd.Series([1, pd.NA, 4], dtype="Int64") |
| 18 | + f = s.astype("float64") |
| 19 | + assert f.dtype == "float64" |
| 20 | + # float representation should have np.nan where original had <NA> |
| 21 | + assert np.isnan(f.iloc[1]) |
| 22 | + # roundtrip back to nullable Int64 |
| 23 | + back = f.astype("Int64") |
| 24 | + expected = pd.Series([1, pd.NA, 4], dtype="Int64") |
| 25 | + tm.assert_series_equal(back, expected) |
| 26 | + |
| 27 | +@pytest.mark.parametrize("to_dtype", ["Int32", "Int64", "Float32", "Float64") |
| 28 | +def test_nullable_series_astype_various_dtypes_preserve_missing(to_dtype): |
| 29 | + # small matrix of cases: ensure missingness preserved when casting between |
| 30 | + # nullable integer/float dtypes and non-nullable numpy float dtypes |
| 31 | + s = pd.Series([0, 1, pd.NA, 3], dtype="Int64") |
| 32 | + res = s.astype(to_dtype) |
| 33 | + if to_dtype.startswith("Int"): |
| 34 | + # result should be nullable integer with pd.NA retained |
| 35 | + assert str(res.dtype).startswith("Int") |
| 36 | + expected = pd.Series([0, 1, pd.NA, 3], dtype=to_dtype) |
| 37 | + tm.assert_series_equal(res, expected) |
| 38 | + else: |
| 39 | + # float dtypes: missingness becomes np.nan and dtype is numpy float |
| 40 | + assert "Float" in to_dtype or to_dtype.startswith("float") or res.dtype.kind == "f" |
| 41 | + assert np.isnan(res.iloc[2]) |
0 commit comments