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

Chao YUE chaoyuejoy at gmail.com
Tue May 22 16:47:21 EDT 2012


Thanks Chris for informative post.

cheers,

Chao

2012/5/22 Chris Barker <chris.barker at noaa.gov>

> On Tue, May 22, 2012 at 6:33 AM, Chao YUE <chaoyuejoy at gmail.com> wrote:
>
> > Just in case some one didn't know this. Assign a float number to an
> integer
> > array element will always return integer.
>
> right -- numpy arrays are typed -- that's one of the points of them --
> you wouldn't want the entire array up-cast with a single assignment --
> particularly since there are only python literals for a subset of the
> numpy types.
>
> > so I would always do this if I expected a transfer from integer to float?
> > In [18]: b=a.astype(float)
>
> yes -- but that's an odd way of thinking about -- what you want to do
> is think about what type you need your array to be before you create
> it, then create it the way you need it:
>
> In [87]: np.arange(5, dtype=np.float)
> Out[87]: array([ 0.,  1.,  2.,  3.,  4.])
>
> or better:
>
> In [91]: np.linspace(0,5,6)
> Out[91]: array([ 0.,  1.,  2.,  3.,  4.,  5.])
>
> note that most (all?) numpy array constructors take a "dtype" argument.
>
> -Chris
>
>
>
>
>
> > 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
> >
> ************************************************************************************
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
>
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
***********************************************************************************
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/58ee2955/attachment.html>


More information about the NumPy-Discussion mailing list