[AstroPy] Problem with astropy 03 and quantities

Yannick Roehlly yannick.roehlly at lam.fr
Mon Nov 25 05:14:53 EST 2013

Hi Thomas,

Thanks for the answer.

Le Mon, 25 Nov 2013 10:47:37 +0100,
Thomas Robitaille <thomas.robitaille at gmail.com> a écrit :

> First, regarding the ``copy`` issue, could you provide some code to
> replicate this? ``copy`` should work with Quantites:
>     In [5]: np.copy(np.array([1,2,3]) * u.m)
>     Out[5]: array([1, 2, 3])

np.copy should accept an array-like but here it fails if it's an array
of quantities.

 In [16]: np.copy([x * u.m for x in xrange(10)])
 ERROR:astropy:ValueError: setting an array element with a sequence.
 ERROR: ValueError: setting an array element with a sequence.
 ValueError                                Traceback (most recent call
 last) <ipython-input-16-dd73be22bb28> in <module>() ----> 1 np.copy([x
 * u.m for x in xrange(10)])
 /usr/lib/pymodules/python2.7/numpy/lib/function_base.pyc in copy(a,
 order) 828 
     829     """
 --> 830     return array(a, order=order, copy=True)
     832 # Basic operations
 ValueError: setting an array element with a sequence.

I understand that this is not a bug as everything works as expected and
as numpy can't manage a Quantity datatype.

> To adapt your code, you simply need to add ``.value`` to the quantity
> to get the underlying Numpy array if you want to drop the units.

Yes, but that means the code will not work with prior version of
astropy. I'd better change it to have a numpy.array instead of a list
at the point the copy fails.

> There is a performance penalty in using Quantity in 0.3, but already
> in 0.3.1 there will be significant optimizations, and given that
> ``Quantity`` is widely used in Astropy, you can expect more work on
> optimizations in future.

My question was not for the astropy internals but for the computation
on arrays. Thanks to the pointer Kyle gave me, I've seen that some
optimizations made by Alex consist in splitting the value from the
quantity, making the computations and joining them back.

> By the way, there *is* an entry in the changelog:

I've read it ;-) but I meant a warning, as it can break some code. But
I admit I'm in a specific case.



One can't proceed from the informal to the formal by formal means.

More information about the AstroPy mailing list