[Numpy-discussion] asarray() behaviour.

Charles R Harris charlesr.harris at gmail.com
Mon Jan 8 22:17:19 EST 2007

On 1/8/07, Tim Leslie <tim.leslie at gmail.com> wrote:
> I get different behaviour from asarray depending on whether I pass in
> a list or an array, Additionally, neither of them give me the expected
> results, which is a TypeError complaining about being unable to use
> complex values as floats. Can someone shed some light on this.

As to the type, the numbers are automatically cast without warning. It is
useful to be able to force this, and the easiest is by default without
raising an error. It is also the Numarray way when using just the array
constructor. The cast for complex numbers works the same way as the
corresponding .astype() method. I suppose it might make sense to raise a
warning when converting complex to real. Anyway, the following is expected.

In [11]: a = arange(3).astype(double)

In [12]: b = asarray(a, dtype=float32)

In [13]: a = a + 1j

In [14]: b = asarray(a, dtype=float32)

Note that the cast to single precision from double also loses precision
without warning. Given that, the fact that

In [16]: b = array([1j, 2, 3], dtype=float32)

raises an error should probably be considered a bug due to 1j not being
recognized as a number.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070108/a6a15a6c/attachment.html>

More information about the NumPy-Discussion mailing list