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

Robert Kern robert.kern@gmail....
Tue Sep 21 18:00:06 CDT 2010

On Tue, Sep 21, 2010 at 17:40, Pauli Virtanen <pav@iki.fi> wrote:
> Tue, 21 Sep 2010 17:28:08 -0500, Robert Kern wrote:
> [clip]
>>> *that* == return a complex number from .real
>> What is the alternative? I'm personally happy with saying that many of
>> the operations we define on numpy arrays can be done because we know the
>> types and that object arrays subvert this. numpy can't, without
>> excessive amounts of magic, always know a sensible thing to do with
>> object arrays, so we implement the fast thing to do.
> As I see it, the alternatives are
> 1) Not to define .real and .imag for object arrays
> 2) Define them as elementwise .real and .imag
> I don't clearly see the reason for
>>>> x.real is x
> True
>>>> x.imag
> array([0], dtype=object)

The reason it is that way is probably because object and str dtypes
were an afterthought when we extended .real and .imag to all dtypes.
We used to only have those attributes on complex arrays. We added the
behavior described above to all of the other dtypes to allow one to
use the .real and .imag attributes regardless of whether the array was
complex or real. I don't think we gave much thought to what the right
thing to do with object arrays or str arrays. Perhaps we did think
about it and decided that consistency with the other non-complex
dtypes was more important than trying to expensively inspect the
individual objects for .real and .imag attributes.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list