Cast to a NumPy array or ExtensionArray with ‘dtype’.
Typecode or data-type to which the array is cast.
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.
An ExtensionArray if dtype is ExtensionDtype, otherwise a Numpy ndarray with dtype for its dtype.
Examples
>>> arr = pd.array([1, 2, 3])
>>> arr
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64
Casting to another ExtensionDtype returns an ExtensionArray:
>>> arr1 = arr.astype('Float64')
>>> arr1
<FloatingArray>
[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')
© 2008–2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
© 2011–2025, Open source contributors
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/2.3.0/reference/api/pandas.api.extensions.ExtensionArray.astype.html