[Numpy-discussion] Fix for ticket #1973

Charles R Harris charlesr.harris@gmail....
Mon Jan 16 12:20:21 CST 2012


On Mon, Jan 16, 2012 at 8:37 AM, Bruce Southey <bsouthey@gmail.com> wrote:

> **
> On 01/14/2012 04:31 PM, Charles R Harris wrote:
>
> I've put up a pull request for a fix to ticket #1973. Currently the fix
> simply propagates the maskna flag when the *.astype method is called. A
> more complicated option would be to add a maskna keyword to specify whether
> the output is masked or not or propagates the type of the source, but that
> seems overly complex to me.
>
> Thoughts?
>
> Chuck
>
>
> _______________________________________________
> NumPy-Discussion mailing listNumPy-Discussion@scipy.orghttp://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>  Thanks for the correction and as well as the fix. While it worked for
> integer and floats (not complex ones), I got an error when using complex
> dtypes. This error that is also present in array creation of complex
> dtypes. Is this known or a new bug?
>
> If it is new, then we need to identify what functionality should handle
> np.NA but are not working.
>
> Bruce
>
> $ 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__ # pull request version
> '2.0.0.dev-88f9276'
> >>> np.array([1,2], dtype=np.complex)
> array([ 1.+0.j,  2.+0.j])
> >>> np.array([1,2, np.NA], dtype=np.complex)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib64/python2.7/site-packages/numpy/core/numeric.py", line
> 1445, in array_repr
>     ', ', "array(")
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 459, in array2string
>     separator, prefix, formatter=formatter)
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 263, in _array2string
>     suppress_small),
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 724, in __init__
>     self.real_format = FloatFormat(x.real, precision, suppress_small)
> ValueError: Cannot construct a view of data together with the
> NPY_ARRAY_MASKNA flag, the NA mask must be added later
> >>> ca=np.array([1,2], dtype=np.complex, maskna=True)
> >>> ca[1]=np.NA
> >>> ca
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib64/python2.7/site-packages/numpy/core/numeric.py", line
> 1445, in array_repr
>     ', ', "array(")
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 459, in array2string
>     separator, prefix, formatter=formatter)
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 263, in _array2string
>     suppress_small),
>   File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py", line
> 724, in __init__
>     self.real_format = FloatFormat(x.real, precision, suppress_small)
> ValueError: Cannot construct a view of data together with the
> NPY_ARRAY_MASKNA flag, the NA mask must be added later
> >>>
>
>
The location of this problem is easy to find, but the fix isn't completely
trivial as it seems there is no easy way to copy masks between arrays.
There may be, but I haven't found it. Also there is the unfortunate fact
that real and imag are array methods and work for non-complex arrays

In [6]: a = ones(3, 'S')

In [7]: a.real
Out[7]:
array(['1', '1', '1'],
      dtype='|S1')

In [8]: a.imag
Out[8]:
array(['', '', ''],
      dtype='|S1')

which makes a simple view impractical. Not that views seem to work.

Another complication of the NA stuff is that there two types of NA, a
potential multivalued NA, and a simple boolean NA. I think we need to pick
between the two as supporting both makes a mess. Because of the common
complaint about memory usage, I vote for simple boolean which offers the
option of bit arrays for the masks.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120116/50be8526/attachment.html 


More information about the NumPy-Discussion mailing list