[AstroPy] why the warning about Converting Quantity?

Adrian Price-Whelan adrianmpw@gmail....
Thu Mar 21 14:05:14 CDT 2013


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





More information about the AstroPy mailing list