def safe_cast(values, dtype, copy):
try:<EOL><INDENT>return values.astype(dtype, casting='<STR_LIT>', copy=copy)<EOL><DEDENT>except TypeError:<EOL><INDENT>casted = values.astype(dtype, copy=copy)<EOL>if (casted == values).all():<EOL><INDENT>return casted<EOL><DEDENT>raise TypeError("<STR_LIT>".format(<EOL>values.dtype, np.dtype(dtype)))<EOL><DEDENT>
Safely cast the values to the dtype if they
are equivalent, meaning floats must be equivalent to the
ints.