[Numpy-discussion] NA masks for NumPy are ready to test

Mark Wiebe mwwiebe at gmail.com
Sat Aug 20 12:26:45 EDT 2011


On Fri, Aug 19, 2011 at 11:37 AM, Bruce Southey <bsouthey at gmail.com> wrote:

> Hi,
> Just some immediate minor observations that are really about trying to
> be consistent:
>
> 1) Could you keep the display of the NA dtype be the same as the array?
> For example, NA dtype is displayed as '<f8' but should be displayed as
> 'float64' as that is the array dtype.
>  >>> a=np.array([[1,2,3,np.NA], [3,4,np.nan,5]])
> >>> a
> array([[  1.,   2.,   3., NA],
>       [  3.,   4.,  nan,   5.]])
> >>> a.dtype
> dtype('float64')
> >>> a.sum()
> NA(dtype='<f8')
>

I've implemented this:

>>> a=np.array([[1,2,3,np.NA], [3,4,np.nan,5]])
>>> a
array([[  1.,   2.,   3.,   NA],
       [  3.,   4.,  nan,   5.]])
>>> a.dtype
dtype('float64')
>>> a.sum()
NA(dtype='float64')


> 2) Can the 'skipna' flag be added to the methods?
> >>> a.sum(skipna=True)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: 'skipna' is an invalid keyword argument for this function
> >>> np.sum(a,skipna=True)
> nan
>
> 3) Can the skipna flag be extended to exclude other non-finite cases like
> NaN?
>
> 4) Assigning a np.NA needs a better error message but the Integer
> array case is more informative:
> >>> b=np.array([1,2,3,4], dtype=np.float128)
> >>> b[0]=np.NA
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: float() argument must be a string or a number
>
> >>> j=np.array([1,2,3])
> >>> j
> array([1, 2, 3])
> >>> j[0]=ina
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: int() argument must be a string or a number, not 'numpy.NAType'
>

Here are the new error messages in these cases:

>>> b=np.array([1,2,3,4], dtype=np.float128)
>>> b[0]=np.NA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Cannot assign NA to an array which does not support NAs
>>> j=np.array([1,2,3])
>>> j[0] = np.NA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Cannot assign NA to an array which does not support NAs

Cheers,
Mark


>
> But it is nice that np.NA 'adjusts' to the insertion array:
> >>> b.flags.maskna = True
> >>> ana
> NA(dtype='<f8')
> >>> b[0]=ana
> >>> b[0]
> NA(dtype='<f16')
>
> 5) Different display depending on masked state. That is I think that
> 'maskna=True' should be displayed always when flags.maskna is True :
> >>> j=np.array([1,2,3], dtype=np.int8)
> >>> j
> array([1, 2, 3], dtype=int8)
> >>> j.flags.maskna=True
> >>> j
> array([1, 2, 3], maskna=True, dtype=int8)
> >>> j[0]=np.NA
> >>> j
> array([NA, 2, 3], dtype=int8) # Ithink it should still display
> 'maskna=True'.
>
> Bruce
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110820/8f13a99e/attachment.html>


More information about the NumPy-Discussion mailing list