pandas.api.extensions.ExtensionArray.astype — pandas 2.2.3 documentation (original) (raw)
ExtensionArray.astype(dtype, copy=True)[source]#
Cast to a NumPy array or ExtensionArray with ‘dtype’.
Parameters:
dtypestr or dtype
Typecode or data-type to which the array is cast.
copybool, default True
Whether to copy the data, even if not necessary. If False, a copy is made only if the old dtype does not match the new dtype.
Returns:
np.ndarray or pandas.api.extensions.ExtensionArray
An ExtensionArray
if dtype
is ExtensionDtype
, otherwise a Numpy ndarray with dtype
for its dtype.
Examples
arr = pd.array([1, 2, 3]) arr [1, 2, 3] Length: 3, dtype: Int64
Casting to another ExtensionDtype
returns an ExtensionArray
:
arr1 = arr.astype('Float64') arr1 [1.0, 2.0, 3.0] Length: 3, dtype: Float64 arr1.dtype Float64Dtype()
Otherwise, we will get a Numpy ndarray:
arr2 = arr.astype('float64') arr2 array([1., 2., 3.]) arr2.dtype dtype('float64')