def astype(self, dtype, copy=True):
dtype = pandas_dtype(dtype)<EOL>if is_interval_dtype(dtype):<EOL><INDENT>if dtype == self.dtype:<EOL><INDENT>return self.copy() if copy else self<EOL><DEDENT>try:<EOL><INDENT>new_left = self.left.astype(dtype.subtype)<EOL>new_right = self.right.astype(dtype.subtype)<EOL><DEDENT>except TypeError:<EOL><INDENT>msg = ('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>raise TypeError(msg.format(dtype=self.dtype, new_dtype=dtype))<EOL><DEDENT>return self._shallow_copy(new_left, new_right)<EOL><DEDENT>elif is_categorical_dtype(dtype):<EOL><INDENT>return Categorical(np.asarray(self))<EOL><DEDENT>try:<EOL><INDENT>return np.asarray(self).astype(dtype, copy=copy)<EOL><DEDENT>except (TypeError, ValueError):<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise TypeError(msg.format(name=type(self).__name__, dtype=dtype))<EOL><DEDENT>
Cast to an ExtensionArray or NumPy array with dtype 'dtype'.

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
-------
array : ExtensionArray or ndarray
    ExtensionArray or NumPy ndarray with 'dtype' for its dtype.