Skip to content

BUG: astype falsely converts inf to integer, patch for Numpy (GH14265) #14343

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
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ Performance Improvements

Bug Fixes
~~~~~~~~~

- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
2 changes: 1 addition & 1 deletion pandas/sparse/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def test_astype(self):
arr.astype('i8')

arr = SparseArray([0, np.nan, 0, 1], fill_value=0)
msg = "Cannot convert NA to integer"
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'
with tm.assertRaisesRegexp(ValueError, msg):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the message the same?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if u want to assert the new message would be great

arr.astype('i8')

Expand Down
14 changes: 11 additions & 3 deletions pandas/tests/frame/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,17 @@ def test_astype_with_view(self):
tf = self.frame.astype(np.float64)
casted = tf.astype(np.int64, copy=False) # noqa

def test_astype_cast_nan_int(self):
df = DataFrame(data={"Values": [1.0, 2.0, 3.0, np.nan]})
self.assertRaises(ValueError, df.astype, np.int64)
def test_astype_cast_nan_inf_int(self):
# GH14265, check nan and inf raise error when converting to int
types = [np.int32, np.int64]
values = [np.nan, np.inf]
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'

for this_type in types:
for this_val in values:
df = DataFrame([this_val])
with tm.assertRaisesRegexp(ValueError, msg):
df.astype(this_type)

def test_astype_str(self):
# GH9757
Expand Down
14 changes: 11 additions & 3 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ def test_dtype(self):
assert_series_equal(self.ts.get_ftype_counts(), Series(
1, ['float64:dense']))

def test_astype_cast_nan_int(self):
df = Series([1.0, 2.0, 3.0, np.nan])
self.assertRaises(ValueError, df.astype, np.int64)
def test_astype_cast_nan_inf_int(self):
# GH14265, check nan and inf raise error when converting to int
types = [np.int32, np.int64]
values = [np.nan, np.inf]
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'

for this_type in types:
for this_val in values:
s = Series([this_val])
with self.assertRaisesRegexp(ValueError, msg):
s.astype(this_type)

def test_astype_cast_object_int(self):
arr = Series(["car", "house", "tree", "1"])
Expand Down
6 changes: 4 additions & 2 deletions pandas/types/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,10 @@ def _astype_nansafe(arr, dtype, copy=True):
elif (np.issubdtype(arr.dtype, np.floating) and
np.issubdtype(dtype, np.integer)):

if np.isnan(arr).any():
raise ValueError('Cannot convert NA to integer')
if not np.isfinite(arr).all():
raise ValueError('Cannot convert non-finite values (NA or inf) to '
'integer')

elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer):
# work around NumPy brokenness, #1987
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
Expand Down