def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bool):
    "\n    Convert input to numpy ndarray and optionally cast to a given dtype.\n\n    Parameters\n    ----------\n    arr : ndarray, scalar, list, tuple, iterator (catchall)\n        Excludes: ExtensionArray, Series, Index.\n    dtype : np.dtype, ExtensionDtype or None\n    copy : bool\n        If False, don't copy the data if not needed.\n    raise_cast_failure : bool\n        If True, and if a dtype is specified, raise errors during casting.\n        Otherwise an object array is returned.\n    "
    if isinstance(arr, np.ndarray):
        if (maybe_castable(arr) and (not copy) and (dtype is None)):
            return arr
    if (isinstance(dtype, ExtensionDtype) and ((dtype.kind != 'M') or is_sparse(dtype))):
        array_type = dtype.construct_array_type()._from_sequence
        subarr = array_type(arr, dtype=dtype, copy=copy)
        return subarr
    try:
        if is_integer_dtype(dtype):
            maybe_cast_to_integer_array(arr, dtype)
            subarr = arr
        else:
            subarr = maybe_cast_to_datetime(arr, dtype)
        if (is_object_dtype(dtype) and (is_list_like(subarr) and (not (is_iterator(subarr) or isinstance(subarr, np.ndarray))))):
            subarr = construct_1d_object_array_from_listlike(subarr)
        elif (not is_extension_array_dtype(subarr)):
            subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy)
    except OutOfBoundsDatetime:
        raise
    except (ValueError, TypeError):
        if ((dtype is not None) and raise_cast_failure):
            raise
        else:
            subarr = np.array(arr, dtype=object, copy=copy)
    return subarr
Convert input to numpy ndarray and optionally cast to a given dtype.

Parameters
----------
arr : ndarray, scalar, list, tuple, iterator (catchall)
    Excludes: ExtensionArray, Series, Index.
dtype : np.dtype, ExtensionDtype or None
copy : bool
    If False, don't copy the data if not needed.
raise_cast_failure : bool
    If True, and if a dtype is specified, raise errors during casting.
    Otherwise an object array is returned.
def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bool):
    "\n    Convert input to numpy ndarray and optionally cast to a given dtype.\n\n    Parameters\n    ----------\n    arr : ndarray, scalar, list, tuple, iterator (catchall)\n        Excludes: ExtensionArray, Series, Index.\n    dtype : np.dtype, ExtensionDtype or None\n    copy : bool\n        If False, don't copy the data if not needed.\n    raise_cast_failure : bool\n        If True, and if a dtype is specified, raise errors during casting.\n        Otherwise an object array is returned.\n    "
    if isinstance(arr, np.ndarray):
        if (maybe_castable(arr) and (not copy) and (dtype is None)):
            return arr
    if (isinstance(dtype, ExtensionDtype) and ((dtype.kind != 'M') or is_sparse(dtype))):
        array_type = dtype.construct_array_type()._from_sequence
        subarr = array_type(arr, dtype=dtype, copy=copy)
        return subarr
    try:
        if is_integer_dtype(dtype):
            maybe_cast_to_integer_array(arr, dtype)
            subarr = arr
        else:
            subarr = maybe_cast_to_datetime(arr, dtype)
        if (is_object_dtype(dtype) and (is_list_like(subarr) and (not (is_iterator(subarr) or isinstance(subarr, np.ndarray))))):
            subarr = construct_1d_object_array_from_listlike(subarr)
        elif (not is_extension_array_dtype(subarr)):
            subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy)
    except OutOfBoundsDatetime:
        raise
    except (ValueError, TypeError):
        if ((dtype is not None) and raise_cast_failure):
            raise
        else:
            subarr = np.array(arr, dtype=object, copy=copy)
    return subarr