简体   繁体   中英

Recasting NumPy array slice with astype

The code

a = 100. * np.random.randn(200)                                                                        
a = a.astype(int)  

recasts a as an array of integer numbers. Meanwhile, the code

a = 100. * np.random.randn(200)                                                                        
a[:] = a.astype(int) 

recasts a as an array of integer floats obtained by truncating the original values of a . Why doesn't the second case also give integers?

I'm currently using Python 3.8 and NumPy 1.23.

To clarify, a.astype(int) return a new array with dtype('int32') .

  • Line a = a.astype(int) alters array a with the new array return by astype(int) .

  • Meanwhile, a[:] = a.astype(int) only update the values of the a array, not the dtype.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM