[Numpy-discussion] real and imag functions should produce errors for object arrays

Matthew Brett matthew.brett@gmail....
Tue Sep 21 16:50:54 CDT 2010


Hi,

On Tue, Sep 21, 2010 at 2:44 PM, Benjamin Root <ben.root@ou.edu> wrote:
> On Tue, Sep 21, 2010 at 4:31 PM, Michael Gilbert
> <michael.s.gilbert@gmail.com> wrote:
>>
>> Hi,
>>
>> The following example demonstrates a rather unexpected result:
>>
>> >>> import numpy
>> >>> x = numpy.array( complex( 1.0 , 1.0 ) , numpy.object )
>> >>> print x.real
>> (1+1j)
>> >>> print x.imag
>> 0
>>
>> Shouldn't real and imag return an error in such a situation?

It looks like there was a decision to let 'real' and 'imag' pass
quietly for non-numerical types:

In [2]: a = np.array('hello', dtype=object)

In [3]: a.real
Out[3]: array('hello', dtype=object)

In [4]: a.imag
Out[4]: array(0, dtype=object)

and

In [6]: a = np.array('hello', dtype='S5')

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

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

I can see that that could be confusing.   I suppose the alternative
would be to raise an error for real and imag for non-numerical types
at least.

Best,

Matthew


More information about the NumPy-Discussion mailing list