Skip to content

Commit 5b85470

Browse files
shawnheideischurov
authored andcommitted
BUG: astype falsely converts inf to integer (GH14265) (pandas-dev#14343)
1 parent 9529ec0 commit 5b85470

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,5 @@ Performance Improvements
118118

119119
Bug Fixes
120120
~~~~~~~~~
121+
122+
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)

pandas/sparse/tests/test_array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def test_astype(self):
361361
arr.astype('i8')
362362

363363
arr = SparseArray([0, np.nan, 0, 1], fill_value=0)
364-
msg = "Cannot convert NA to integer"
364+
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'
365365
with tm.assertRaisesRegexp(ValueError, msg):
366366
arr.astype('i8')
367367

pandas/tests/frame/test_dtypes.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,17 @@ def test_astype_with_view(self):
353353
tf = self.frame.astype(np.float64)
354354
casted = tf.astype(np.int64, copy=False) # noqa
355355

356-
def test_astype_cast_nan_int(self):
357-
df = DataFrame(data={"Values": [1.0, 2.0, 3.0, np.nan]})
358-
self.assertRaises(ValueError, df.astype, np.int64)
356+
def test_astype_cast_nan_inf_int(self):
357+
# GH14265, check nan and inf raise error when converting to int
358+
types = [np.int32, np.int64]
359+
values = [np.nan, np.inf]
360+
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'
361+
362+
for this_type in types:
363+
for this_val in values:
364+
df = DataFrame([this_val])
365+
with tm.assertRaisesRegexp(ValueError, msg):
366+
df.astype(this_type)
359367

360368
def test_astype_str(self):
361369
# GH9757

pandas/tests/series/test_dtypes.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ def test_dtype(self):
4242
assert_series_equal(self.ts.get_ftype_counts(), Series(
4343
1, ['float64:dense']))
4444

45-
def test_astype_cast_nan_int(self):
46-
df = Series([1.0, 2.0, 3.0, np.nan])
47-
self.assertRaises(ValueError, df.astype, np.int64)
45+
def test_astype_cast_nan_inf_int(self):
46+
# GH14265, check nan and inf raise error when converting to int
47+
types = [np.int32, np.int64]
48+
values = [np.nan, np.inf]
49+
msg = 'Cannot convert non-finite values \(NA or inf\) to integer'
50+
51+
for this_type in types:
52+
for this_val in values:
53+
s = Series([this_val])
54+
with self.assertRaisesRegexp(ValueError, msg):
55+
s.astype(this_type)
4856

4957
def test_astype_cast_object_int(self):
5058
arr = Series(["car", "house", "tree", "1"])

pandas/types/cast.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,10 @@ def _astype_nansafe(arr, dtype, copy=True):
527527
elif (np.issubdtype(arr.dtype, np.floating) and
528528
np.issubdtype(dtype, np.integer)):
529529

530-
if np.isnan(arr).any():
531-
raise ValueError('Cannot convert NA to integer')
530+
if not np.isfinite(arr).all():
531+
raise ValueError('Cannot convert non-finite values (NA or inf) to '
532+
'integer')
533+
532534
elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.integer):
533535
# work around NumPy brokenness, #1987
534536
return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)

0 commit comments

Comments
 (0)