[NumPy-Tickets] [NumPy] #1890: piecewise throws TypeError for int input and sin

NumPy Trac numpy-tickets@scipy....
Fri Jul 1 16:08:04 CDT 2011


#1890: piecewise throws TypeError for int input and sin
--------------------+-------------------------------------------------------
 Reporter:  deil    |       Owner:  somebody   
     Type:  defect  |      Status:  new        
 Priority:  normal  |   Milestone:  Unscheduled
Component:  Other   |     Version:  1.6.0      
 Keywords:          |  
--------------------+-------------------------------------------------------
 I would have expected the following np.piecewise call to work:
 {{{
 >>> x = np.arange(0, 10)
 >>> np.piecewise(x, [x < 5, x >= 5], [lambda x: -x, lambda x: np.sin(x)])
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File
 "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
 /site-packages/numpy/lib/function_base.py", line 715, in piecewise
     y[condlist[k]] = item(vals, *args, **kw)
 TypeError: array cannot be safely cast to required type
 >>> x.dtype
 dtype('int64')
 }}}

 It works for an array of floats though:
 {{{
 >>> x = np.arange(0, 10, dtype=float)
 >>> np.piecewise(x, [x < 5, x >= 5], [lambda x: -x, lambda x: np.sin(x)])
 array([-0.        , -1.        , -2.        , -3.        , -4.        ,
        -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
 }}}

 Is this a bug?

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1890>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list