[Numpy-discussion] astype does not work with NA object

Bruce Southey bsouthey at gmail.com
Mon Dec 5 12:46:34 EST 2011


Hi,
I mistakenly filed ticket 1973 "Can not display a masked array 
containing np.NA values even if masked" 
(http://projects.scipy.org/numpy/ticket/1973) against masked array 
because that was where I found it. But the actual error is that the 
astype function does not handle the NA object:

$ python
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import numpy as np
 >>> np.__version__
'2.0.0.dev-059334c'
 >>> np.array([1,2,3,4]).astype(float)
array([ 1.,  2.,  3.,  4.])
 >>> np.array([1,2,3,np.NA]).astype(float)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: Cannot assign NA to an array which does not support NAs
 >>> a=np.array([1,2,3,4], maskna=True)
 >>> a[3]=np.NA
 >>> a
array([1, 2, 3, NA])
 >>> a.astype(float)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: Cannot assign NA to an array which does not support NAs
 >>>a*1.0
array([ 1.,  2.,  3.,  NA])

Bruce



More information about the NumPy-Discussion mailing list