Skip to content

BUG: Raise when casting infinity to int #28475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down