diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index a9140fa20ceef..da8d88fe6cbb1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -174,6 +174,7 @@ Indexing - Bug in assignment using a reverse slicer (:issue:`26939`) - Bug in reindexing a :meth:`PeriodIndex` with another type of index that contained a `Period` (:issue:`28323`) (:issue:`28337`) - Fix assignment of column via `.loc` with numpy non-ns datetime type (:issue:`27395`) +- Bug in :meth:`Float64Index.astype` where ``np.inf`` was not handled properly when casting to an integer dtype (:issue:`28475`) Missing ^^^^^^^ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 2cdf73788dd9b..e83360dc701f3 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -5,6 +5,7 @@ from pandas._libs import index as libindex from pandas.util._decorators import Appender, cache_readonly +from pandas.core.dtypes.cast import astype_nansafe from pandas.core.dtypes.common import ( is_bool, is_bool_dtype, @@ -367,12 +368,11 @@ def astype(self, dtype, copy=True): "values are required for conversion" ).format(dtype=dtype) raise TypeError(msg) - elif ( - is_integer_dtype(dtype) and not is_extension_array_dtype(dtype) - ) and self.hasnans: + elif is_integer_dtype(dtype) and not is_extension_array_dtype(dtype): # TODO(jreback); this can change once we have an EA Index type # GH 13149 - raise ValueError("Cannot convert NA to integer") + arr = astype_nansafe(self.values, dtype=dtype) + return Int64Index(arr) return super().astype(dtype, copy=copy) @Appender(_index_shared_docs["_convert_scalar_indexer"]) diff --git a/pandas/tests/indexes/interval/test_astype.py b/pandas/tests/indexes/interval/test_astype.py index 863b8c9082f07..708cd8a4579e8 100644 --- a/pandas/tests/indexes/interval/test_astype.py +++ b/pandas/tests/indexes/interval/test_astype.py @@ -143,7 +143,7 @@ def test_subtype_integer(self, subtype): tm.assert_index_equal(result, expected) # raises with NA - msg = "Cannot convert NA to integer" + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" with pytest.raises(ValueError, match=msg): index.insert(0, np.nan).astype(dtype) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index f246307e63e3b..8bc9783694492 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -242,10 +242,17 @@ def test_astype(self): # GH 13149 for dtype in ["int16", "int32", "int64"]: i = Float64Index([0, 1.1, np.NAN]) - msg = "Cannot convert NA to integer" + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" with pytest.raises(ValueError, match=msg): i.astype(dtype) + def test_cannot_cast_inf_to_int(self): + idx = pd.Float64Index([1, 2, np.inf]) + + msg = r"Cannot convert non-finite values \(NA or inf\) to integer" + with pytest.raises(ValueError, match=msg): + idx.astype(int) + def test_type_coercion_fail(self, any_int_dtype): # see gh-15832 msg = "Trying to coerce float values to integers"