Skip to content

Commit f5157d5

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 d98e982 commit f5157d5

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ Performance Improvements
7979

8080
Bug Fixes
8181
~~~~~~~~~
82+
83+
- Numpy ``astype()`` caused `inf` values to be falsely converted to integers. Affected ``astype()`` in 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/types/test_cast.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from datetime import datetime
1111
import numpy as np
1212

13-
from pandas import Timedelta, Timestamp
13+
from pandas import Timedelta, Timestamp, Series
1414
from pandas.types.cast import (_possibly_downcast_to_dtype,
1515
_possibly_convert_objects,
1616
_infer_dtype_from_scalar,
@@ -203,6 +203,18 @@ def test_possibly_convert_objects_copy(self):
203203
self.assertTrue(values is not out)
204204

205205

206+
class TestAsType(tm.TestCase):
207+
208+
def test_astype_raises(self):
209+
types = [np.int32, np.int64]
210+
values = [np.nan, np.inf]
211+
212+
for this_type in types:
213+
for this_val in values:
214+
s = Series([this_val])
215+
self.assertRaises(ValueError, s.astype, this_type)
216+
217+
206218
class TestCommonTypes(tm.TestCase):
207219

208220
def test_numpy_dtypes(self):

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)