code: def safe_cast(values, dtype, copy):
      
  
      try:
          return values.astype(dtype, casting='safe', copy=copy)
      except TypeError:
  
          casted = values.astype(dtype, copy=copy)
          if (casted == values).all():
              return casted
  
          raise TypeError("cannot safely cast non-equivalent {} to {}".format(
              values.dtype, np.dtype(dtype)))
docs: Safely cast the values to the dtype if they
      are equivalent, meaning floats must be equivalent to the
      ints.