NumPy ndarray data type

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12244852.html

 

dtype

The data type or dtype is a special object containing the information (or metadata, data about data) the ndarray needs to interpret a chunk of memory as a particular type of data:

 

NumPy data types

you can use shorthand type code strings to create ndarray

arr = np.array([1.1, 2.2, 3.3], dtype='f8')

 

astype

You can explicitly convert or cast an array from one dtype to another using ndarray’s astype method. 

Calling astype always creates a new array (a copy of the data), even if the new dtype is the same as the old dtype.

integer -> float

float -> integer

string -> float

Note: If casting were to fail for some reason (like a string that cannot be converted to float64), a ValueError will be raised.

 

Reference

Python for Data Analysis Second Edition

 

posted @ 2020-01-31 11:20  李白与酒  阅读(1039)  评论(0编辑  收藏  举报