[Numpy-discussion] assign a float number to a member of integer array always return integer

Chao YUE chaoyuejoy at gmail.com
Tue May 22 09:33:21 EDT 2012


Dear all,

Just in case some one didn't know this. Assign a float number to an integer
array element will always return integer.

In [4]: a=np.arange(2,11,2)

In [5]: a
Out[5]: array([ 2,  4,  6,  8, 10])

In [6]: a[1]=4.5

In [7]: a
Out[7]: array([ 2,  4,  6,  8, 10])

so I would always do this if I expected a transfer from integer to float?
In [18]: b=a.astype(float)

In [19]: b
Out[19]: array([  2.,   4.,   6.,   8.,  10.])

In [20]: b[1]=4.5

In [21]: b
Out[21]: array([  2. ,   4.5,   6. ,   8. ,  10. ])

thanks et cheers,

Chao
-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120522/1ec495ba/attachment.html>


More information about the NumPy-Discussion mailing list