def maybe_cast_to_integer_array(arr, dtype, copy=False):
try:<EOL><INDENT>if not hasattr(arr, "<STR_LIT>"):<EOL><INDENT>casted = np.array(arr, dtype=dtype, copy=copy)<EOL><DEDENT>else:<EOL><INDENT>casted = arr.astype(dtype, copy=copy)<EOL><DEDENT><DEDENT>except OverflowError:<EOL><INDENT>raise OverflowError("<STR_LIT>"<EOL>"<STR_LIT>".format(dtype=dtype))<EOL><DEDENT>if np.array_equal(arr, casted):<EOL><INDENT>return casted<EOL><DEDENT>arr = np.asarray(arr)<EOL>if is_unsigned_integer_dtype(dtype) and (arr < <NUM_LIT:0>).any():<EOL><INDENT>raise OverflowError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if is_integer_dtype(dtype) and (is_float_dtype(arr) or<EOL>is_object_dtype(arr)):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>
Takes any dtype and returns the casted version, raising for when data is
incompatible with integer/unsigned integer dtypes.

.. versionadded:: 0.24.0

Parameters
----------
arr : array-like
    The array to cast.
dtype : str, np.dtype
    The integer dtype to cast the array to.
copy: boolean, default False
    Whether to make a copy of the array before returning.

Returns
-------
int_arr : ndarray
    An array of integer or unsigned integer dtype

Raises
------
OverflowError : the dtype is incompatible with the data
ValueError : loss of precision has occurred during casting

Examples
--------
If you try to coerce negative values to unsigned integers, it raises:

>>> Series([-1], dtype="uint64")
Traceback (most recent call last):
    ...
OverflowError: Trying to coerce negative values to unsigned integers

Also, if you try to coerce float values to integers, it raises:

>>> Series([1, 2, 3.5], dtype="int64")
Traceback (most recent call last):
    ...
ValueError: Trying to coerce float values to integers