pandas.api.extensions.ExtensionArray.astype#
- ExtensionArray.astype(dtype, copy=True)[源代码]#
转换为具有 ‘dtype’ 的 NumPy 数组或 ExtensionArray。
- Parameters:
- dtypestr 或 dtype
要转换为的类型码或数据类型。
- copybool, default True
是否复制数据,即使没有必要。如果为 False,则仅在旧 dtype 与新 dtype 不匹配时才进行复制。
- Returns:
- np.ndarray 或 pandas.api.extensions.ExtensionArray
如果
dtype是ExtensionDtype,则返回ExtensionArray,否则返回具有dtype作为其 dtype 的 Numpy ndarray。
Examples
>>> arr = pd.array([1, 2, 3]) >>> arr <IntegerArray> [1, 2, 3] Length: 3, dtype: Int64
转换为另一个
ExtensionDtype会返回一个ExtensionArray:>>> arr1 = arr.astype('Float64') >>> arr1 <FloatingArray> [1.0, 2.0, 3.0] Length: 3, dtype: Float64 >>> arr1.dtype Float64Dtype()
否则,我们将得到一个 Numpy ndarray:
>>> arr2 = arr.astype('float64') >>> arr2 array([1., 2., 3.]) >>> arr2.dtype dtype('float64')