[AstroPy] why the warning about Converting Quantity?

Wolfgang Kerzendorf wkerzendorf@gmail....
Thu Mar 21 14:15:26 CDT 2013


Hi Jonathan,

I think, one should not turn this warning off. It warns you that the quantity you get after the operations does not necessarily have the same units as before - and rightly so. 
When doing things like np.sqrt(15* u.km), they can be replaced with np.sqrt((15 * u.km).value) which specifically tells it to convert to a float (and puts the onus on you to think about the units). 

Cheers
   Wolfgang

Hope that helps
On 2013-03-21, at 3:05 PM, Adrian Price-Whelan <adrianmpw@gmail.com> wrote:

> Hey Jonathan --
> 
> Are you sure that's the point in the code that's producing the warning? What operations are you doing with the AU object? When you use the .to() method, it still returns a Quantity object -- *not* the value of the original object in those units. If you then stick AU into any numpy function, it will work but will just extract the value from the object (hence the warning). 
> 
> See, for example:
> 
>>>> import astropy.units as u
>>>> np.sqrt(15*u.km)
> WARNING: Converting Quantity object in units 'km' to a Numpy array [astropy.units.quantity]
> 3.872983346207417
> 
> To turn this off, you'll want to set:
>>>> u.quantity.WARN_IMPLICIT_NUMERIC_CONVERSION.set(False)
>>>> np.sqrt(15*u.km)
> 3.872983346207417
> 
> Note that this is documented in the astropy.units.quantity section of the astropy documentation:
> http://docs.astropy.org/en/latest/units/quantity.html#converting-to-python-or-numpy-types
> 
> Thanks,
> Adrian
> 
> On Mar 21, 2013, at 2:20 PM, Jonathan Slavin wrote:
> 
>> Hi,
>> 
>> I have just used the constants module in astropy for the first time and
>> got 6 warnings from my code as a result:
>> 
>> import astropy.constants as const
>> AU = const.au.to('km')
>> 
>> leads to 
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> WARNING: Converting Quantity object in units 'km' to a Numpy array
>> [astropy.units.quantity]
>> 
>> It seems that I get the warning every place I use the value of AU.  Do I
>> have to turn off warnings  to prevent this?  Is there some other way to
>> deal with this?
>> 
>> Jon
>> -- 
>> ______________________________________________________________
>> Jonathan D. Slavin              Harvard-Smithsonian CfA
>> jslavin@cfa.harvard.edu         60 Garden Street, MS 83
>> phone: (617) 496-7981           Cambridge, MA 02138-1516
>> cell: (781) 363-0035           USA
>> ______________________________________________________________
>> 
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy@scipy.org
>> http://mail.scipy.org/mailman/listinfo/astropy
> 
> --
> Adrian Price-Whelan
> Department of Astronomy
> Columbia University
> 
> 
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy@scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy



More information about the AstroPy mailing list