From d43bcc3811bc4930e80aed54d2a491dabe24fd8f Mon Sep 17 00:00:00 2001 From: Daniel Saxton <> Date: Mon, 16 Sep 2019 21:43:30 -0400 Subject: [PATCH 1/4] BUG: Raise when casting infinity to int --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/core/indexes/numeric.py | 9 +++++---- pandas/tests/indexes/test_numeric.py | 6 ++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index a9140fa20ceef..e41b47f91b1f4 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 Missing ^^^^^^^ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 2cdf73788dd9b..dcde5fb1652d7 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -367,12 +367,13 @@ 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") + if self.hasnans: + raise ValueError("Cannot convert NA to integer") + elif not np.isfinite(self).all(): + raise ValueError("Cannot convert infinity to integer") return super().astype(dtype, copy=copy) @Appender(_index_shared_docs["_convert_scalar_indexer"]) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index f246307e63e3b..d5b18d75aa6bc 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -246,6 +246,12 @@ def test_astype(self): with pytest.raises(ValueError, match=msg): i.astype(dtype) + def test_cannot_cast_inf_to_int(self): + idx = pd.Float64Index([1, 2, np.inf]) + + with pytest.raises(ValueError, match="Cannot convert infinity to integer"): + idx.astype(int) + def test_type_coercion_fail(self, any_int_dtype): # see gh-15832 msg = "Trying to coerce float values to integers" From 5d3714a2200a50f193ed756303d9a04eb5ce49e6 Mon Sep 17 00:00:00 2001 From: Daniel Saxton <> Date: Tue, 17 Sep 2019 08:37:00 -0400 Subject: [PATCH 2/4] Add PR number --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index e41b47f91b1f4..da8d88fe6cbb1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -174,7 +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 +- Bug in :meth:`Float64Index.astype` where ``np.inf`` was not handled properly when casting to an integer dtype (:issue:`28475`) Missing ^^^^^^^ From d4ea3ea92998901e1f587a7659a56103891d3b0c Mon Sep 17 00:00:00 2001 From: Daniel Saxton <> Date: Tue, 17 Sep 2019 09:03:25 -0400 Subject: [PATCH 3/4] Use astype_nansafe --- pandas/core/indexes/numeric.py | 7 +++---- pandas/tests/indexes/test_numeric.py | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index dcde5fb1652d7..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, @@ -370,10 +371,8 @@ def astype(self, dtype, copy=True): 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 - if self.hasnans: - raise ValueError("Cannot convert NA to integer") - elif not np.isfinite(self).all(): - raise ValueError("Cannot convert infinity 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/test_numeric.py b/pandas/tests/indexes/test_numeric.py index d5b18d75aa6bc..8bc9783694492 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -242,14 +242,15 @@ 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]) - with pytest.raises(ValueError, match="Cannot convert infinity to integer"): + 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): From fd70d729db15c474e065e9442400f5651e982593 Mon Sep 17 00:00:00 2001 From: Daniel Saxton <> Date: Tue, 17 Sep 2019 10:16:22 -0400 Subject: [PATCH 4/4] Fix error message pattern in test --- pandas/tests/indexes/interval/test_astype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)