|
18 | 18 | import pytest
|
19 | 19 | import pytz
|
20 | 20 |
|
| 21 | +from pandas.errors import IntCastingNaNError |
21 | 22 | import pandas.util._test_decorators as td
|
22 | 23 |
|
23 | 24 | from pandas.core.dtypes.common import is_integer_dtype
|
@@ -105,16 +106,13 @@ def test_constructor_dict_with_tzaware_scalar(self):
|
105 | 106 | def test_construct_ndarray_with_nas_and_int_dtype(self):
|
106 | 107 | # GH#26919 match Series by not casting np.nan to meaningless int
|
107 | 108 | arr = np.array([[1, np.nan], [2, 3]])
|
108 |
| - with tm.assert_produces_warning(FutureWarning): |
109 |
| - df = DataFrame(arr, dtype="i8") |
110 |
| - assert df.values.dtype == arr.dtype |
111 |
| - assert isna(df.iloc[0, 1]) |
| 109 | + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" |
| 110 | + with pytest.raises(IntCastingNaNError, match=msg): |
| 111 | + DataFrame(arr, dtype="i8") |
112 | 112 |
|
113 | 113 | # check this matches Series behavior
|
114 |
| - with tm.assert_produces_warning(FutureWarning): |
115 |
| - ser = Series(arr[0], dtype="i8", name=0) |
116 |
| - expected = df.iloc[0] |
117 |
| - tm.assert_series_equal(ser, expected) |
| 114 | + with pytest.raises(IntCastingNaNError, match=msg): |
| 115 | + Series(arr[0], dtype="i8", name=0) |
118 | 116 |
|
119 | 117 | def test_construct_from_list_of_datetimes(self):
|
120 | 118 | df = DataFrame([datetime.now(), datetime.now()])
|
@@ -966,21 +964,16 @@ def _check_basic_constructor(self, empty):
|
966 | 964 | assert len(frame.index) == 3
|
967 | 965 | assert len(frame.columns) == 1
|
968 | 966 |
|
969 |
| - warn = None if empty is np.ones else FutureWarning |
970 |
| - with tm.assert_produces_warning(warn): |
| 967 | + if empty is not np.ones: |
| 968 | + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" |
| 969 | + with pytest.raises(IntCastingNaNError, match=msg): |
| 970 | + DataFrame(mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64) |
| 971 | + return |
| 972 | + else: |
971 | 973 | frame = DataFrame(
|
972 | 974 | mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64
|
973 | 975 | )
|
974 |
| - if empty is np.ones: |
975 |
| - # passing dtype casts |
976 | 976 | assert frame.values.dtype == np.int64
|
977 |
| - else: |
978 |
| - # i.e. ma.masked_all |
979 |
| - # Since we have NaNs, refuse to cast to int dtype, which would take NaN |
980 |
| - # to meaningless integers. This matches Series behavior. GH#26919 |
981 |
| - assert frame.isna().all().all() |
982 |
| - assert frame.values.dtype == np.float64 |
983 |
| - assert isna(frame.values).all() |
984 | 977 |
|
985 | 978 | # wrong size axis labels
|
986 | 979 | msg = r"Shape of passed values is \(2, 3\), indices imply \(1, 3\)"
|
@@ -1741,11 +1734,10 @@ def test_constructor_mix_series_nonseries(self, float_frame):
|
1741 | 1734 | DataFrame({"A": float_frame["A"], "B": list(float_frame["B"])[:-2]})
|
1742 | 1735 |
|
1743 | 1736 | def test_constructor_miscast_na_int_dtype(self):
|
1744 |
| - msg = "float-dtype values containing NaN and an integer dtype" |
1745 |
| - with tm.assert_produces_warning(FutureWarning, match=msg): |
1746 |
| - df = DataFrame([[np.nan, 1], [1, 0]], dtype=np.int64) |
1747 |
| - expected = DataFrame([[np.nan, 1], [1, 0]]) |
1748 |
| - tm.assert_frame_equal(df, expected) |
| 1737 | + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" |
| 1738 | + |
| 1739 | + with pytest.raises(IntCastingNaNError, match=msg): |
| 1740 | + DataFrame([[np.nan, 1], [1, 0]], dtype=np.int64) |
1749 | 1741 |
|
1750 | 1742 | def test_constructor_column_duplicates(self):
|
1751 | 1743 | # it works! #2079
|
@@ -2722,16 +2714,16 @@ def test_floating_values_integer_dtype(self):
|
2722 | 2714 |
|
2723 | 2715 | # with NaNs, we go through a different path with a different warning
|
2724 | 2716 | arr[0, 0] = np.nan
|
2725 |
| - msg = "passing float-dtype values containing NaN" |
2726 |
| - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 2717 | + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" |
| 2718 | + with pytest.raises(IntCastingNaNError, match=msg): |
2727 | 2719 | DataFrame(arr, dtype="i8")
|
2728 |
| - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 2720 | + with pytest.raises(IntCastingNaNError, match=msg): |
2729 | 2721 | Series(arr[0], dtype="i8")
|
2730 | 2722 | # The future (raising) behavior matches what we would get via astype:
|
2731 | 2723 | msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
|
2732 |
| - with pytest.raises(ValueError, match=msg): |
| 2724 | + with pytest.raises(IntCastingNaNError, match=msg): |
2733 | 2725 | DataFrame(arr).astype("i8")
|
2734 |
| - with pytest.raises(ValueError, match=msg): |
| 2726 | + with pytest.raises(IntCastingNaNError, match=msg): |
2735 | 2727 | Series(arr[0]).astype("i8")
|
2736 | 2728 |
|
2737 | 2729 |
|
|
0 commit comments