[NumPy-Tickets] [NumPy] #1709: astype should not raise ComplexWarning

NumPy Trac numpy-tickets@scipy....
Wed Jan 12 05:01:38 CST 2011


#1709: astype should not raise ComplexWarning
------------------------------------+---------------------------------------
 Reporter:  rgommers                |       Owner:  somebody
     Type:  defect                  |      Status:  new     
 Priority:  normal                  |   Milestone:  2.0.0   
Component:  numpy.core              |     Version:  devel   
 Keywords:  astype, complexwarning  |  
------------------------------------+---------------------------------------

Comment(by pv):

 I disagree: the point of the warning is that *all* casts, even explicit,
 from complex to float are ambiguous, and their occurrence is typically a
 sign of a bug. `astype` is as prone to accidentally losing the imaginary
 part as the other routines.

 Suppose that, for instance, an interpolation routine did
 {{{
 def interp(x,y,xi):
     x = np.asarray(y)
     x = np.astype(np.float64)
     y = np.asarray(y)
     y = y.astype(np.float64)
     xi = np.asarray(y)
     xi = np.astype(np.float64)
     return do_float64_interp(x, y, xi)
 }}}
 This would fail silently for complex-valued data, if explicit casts were
 silent. Note that this is not a wholly syntetic example: IIRC, some of the
 interpolation routines in `scipy.interpolate` used to have this error.

 To be explicit about the real part, one should do `x.real.astype(...)`
 instead.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1709#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list