Data type array library numpy

Data type array library numpy

dtype is a special object that will be a memory comprising ndarray interpreted as special data type information needed

Specify the data type to create an array

>>> import numpy as np
>>> arr1=np.array([1,2,3,4],dtype=np.float64)
>>> arr2=np.array([1,2,3,4],dtype=np.int32)
>>> arr1.dtype
dtype('float64')
>>> arr2.dtype
dtype('int32')

numpy data types

Array data type conversion

>>> import numpy as np
>>> arr=np.array([1,2,3,4,5])
>>> arr.dtype
dtype('int32')
>>> float_arr=arr.astype(np.float64)
>>> float_arr
array([1., 2., 3., 4., 5.])
>>> float_arr.dtype
dtype('float64')
>>> arr_string=np.array(['1.24','2.6','21'],dtype=np.string_)
>>> arr_string.astype(float)
array([ 1.24,  2.6 , 21.  ])

note:

1. Use numpy.string_ type, must be careful because NumPy string data is a fixed size, interception occurs without warning. pandas provides a convenient method of handling more non-numerical data.

2. Call astype always create a new array (a data backup), even when the same new and old dtype dtype.

Guess you like

Origin www.cnblogs.com/mengxiaoleng/p/11616270.html