Skip to content

Commit 367670e

Browse files
dsaxtonjreback
authored andcommitted
BUG: Raise when casting infinity to int (#28475)
1 parent d38627b commit 367670e

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Indexing
184184
- Bug in :meth:`DataFrame.explode` would duplicate frame in the presence of duplicates in the index (:issue:`28010`)
185185
- Bug in reindexing a :meth:`PeriodIndex` with another type of index that contained a `Period` (:issue:`28323`) (:issue:`28337`)
186186
- Fix assignment of column via `.loc` with numpy non-ns datetime type (:issue:`27395`)
187+
- Bug in :meth:`Float64Index.astype` where ``np.inf`` was not handled properly when casting to an integer dtype (:issue:`28475`)
187188

188189
Missing
189190
^^^^^^^

pandas/core/indexes/numeric.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pandas._libs import index as libindex
66
from pandas.util._decorators import Appender, cache_readonly
77

8+
from pandas.core.dtypes.cast import astype_nansafe
89
from pandas.core.dtypes.common import (
910
is_bool,
1011
is_bool_dtype,
@@ -367,12 +368,11 @@ def astype(self, dtype, copy=True):
367368
"values are required for conversion"
368369
).format(dtype=dtype)
369370
raise TypeError(msg)
370-
elif (
371-
is_integer_dtype(dtype) and not is_extension_array_dtype(dtype)
372-
) and self.hasnans:
371+
elif is_integer_dtype(dtype) and not is_extension_array_dtype(dtype):
373372
# TODO(jreback); this can change once we have an EA Index type
374373
# GH 13149
375-
raise ValueError("Cannot convert NA to integer")
374+
arr = astype_nansafe(self.values, dtype=dtype)
375+
return Int64Index(arr)
376376
return super().astype(dtype, copy=copy)
377377

378378
@Appender(_index_shared_docs["_convert_scalar_indexer"])

pandas/tests/indexes/interval/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_subtype_integer(self, subtype):
143143
tm.assert_index_equal(result, expected)
144144

145145
# raises with NA
146-
msg = "Cannot convert NA to integer"
146+
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
147147
with pytest.raises(ValueError, match=msg):
148148
index.insert(0, np.nan).astype(dtype)
149149

pandas/tests/indexes/test_numeric.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,17 @@ def test_astype(self):
242242
# GH 13149
243243
for dtype in ["int16", "int32", "int64"]:
244244
i = Float64Index([0, 1.1, np.NAN])
245-
msg = "Cannot convert NA to integer"
245+
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
246246
with pytest.raises(ValueError, match=msg):
247247
i.astype(dtype)
248248

249+
def test_cannot_cast_inf_to_int(self):
250+
idx = pd.Float64Index([1, 2, np.inf])
251+
252+
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
253+
with pytest.raises(ValueError, match=msg):
254+
idx.astype(int)
255+
249256
def test_type_coercion_fail(self, any_int_dtype):
250257
# see gh-15832
251258
msg = "Trying to coerce float values to integers"

0 commit comments

Comments
 (0)