Skip to content

Commit 86b905a

Browse files
committed
BUG: astype falsely converts inf to integer, patch for Numpy (GH14265) (+1 squashed commit)
Squashed commits: [56d2ca5] Change sparse test for astype to assertRaises(ValueError) instead of regex version
1 parent d1d75d7 commit 86b905a

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ Performance Improvements
8080

8181
Bug Fixes
8282
~~~~~~~~~
83+
84+
- 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-2
Original file line numberDiff line numberDiff line change
@@ -361,8 +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"
365-
with tm.assertRaisesRegexp(ValueError, msg):
364+
with tm.assertRaises(ValueError):
366365
arr.astype('i8')
367366

368367
def test_astype_all(self):

pandas/tests/frame/test_dtypes.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,15 @@ 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+
361+
for this_type in types:
362+
for this_val in values:
363+
df = DataFrame([this_val])
364+
self.assertRaises(ValueError, df.astype, this_type)
359365

360366
def test_astype_str(self):
361367
# GH9757

pandas/tests/series/test_dtypes.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ 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+
50+
for this_type in types:
51+
for this_val in values:
52+
s = Series([this_val])
53+
self.assertRaises(ValueError, s.astype, this_type)
4854

4955
def test_astype_cast_object_int(self):
5056
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)