Skip to main content
`Ctrl`+`K`

# pandas.api.extensions.ExtensionArray.astype

ExtensionArray.astype(dtype, copy=True)

  Cast to a NumPy array or ExtensionArray with ‘dtype’.

  If the target dtype is an ExtensionDtype, the result is an ExtensionArray. Otherwise, the result is a NumPy ndarray.

  Parameters:
    dtype str or dtype

      Typecode or data-type to which the array is cast.

    copy bool, 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.

  See also

  `Series.astype`

    Cast a Series to a different dtype.

  `DataFrame.astype`

    Cast a DataFrame to a different dtype.

  `api.extensions.ExtensionArray`

    Base class for ExtensionArray objects.

  `core.arrays.DatetimeArray._from_sequence`

    Create a DatetimeArray from a sequence.

  `core.arrays.TimedeltaArray._from_sequence`

    Create a TimedeltaArray from a sequence.

  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')