def sanitize_array(data, index: Optional[Index], dtype: Optional[DtypeObj]=None, copy: bool=False, raise_cast_failure: bool=False) -> ArrayLike:
    '\n    Sanitize input data to an ndarray or ExtensionArray, copy if specified,\n    coerce to the dtype if specified.\n    '
    if isinstance(data, ma.MaskedArray):
        mask = ma.getmaskarray(data)
        if mask.any():
            (data, fill_value) = maybe_upcast(data, copy=True)
            data.soften_mask()
            data[mask] = fill_value
        else:
            data = data.copy()
    data = extract_array(data, extract_numpy=True)
    if isinstance(data, np.ndarray):
        if ((dtype is not None) and is_float_dtype(data.dtype) and is_integer_dtype(dtype)):
            try:
                subarr = _try_cast(data, dtype, copy, True)
            except ValueError:
                if copy:
                    subarr = data.copy()
                else:
                    subarr = np.array(data, copy=False)
        else:
            subarr = _try_cast(data, dtype, copy, raise_cast_failure)
    elif isinstance(data, ABCExtensionArray):
        subarr = data
        if (dtype is not None):
            subarr = subarr.astype(dtype, copy=copy)
        elif copy:
            subarr = subarr.copy()
        return subarr
    elif (isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and (len(data) > 0)):
        if isinstance(data, set):
            raise TypeError('Set type is unordered')
        data = list(data)
        if (dtype is not None):
            subarr = _try_cast(data, dtype, copy, raise_cast_failure)
        else:
            subarr = maybe_convert_platform(data)
        subarr = maybe_cast_to_datetime(subarr, dtype)
    elif isinstance(data, range):
        arr = np.arange(data.start, data.stop, data.step, dtype='int64')
        subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
    elif (lib.is_scalar(data) and (index is not None) and (dtype is not None)):
        data = maybe_cast_to_datetime(data, dtype)
        if (not lib.is_scalar(data)):
            data = data[0]
        subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
    else:
        subarr = _try_cast(data, dtype, copy, raise_cast_failure)
    if (getattr(subarr, 'ndim', 0) == 0):
        if isinstance(data, list):
            subarr = np.array(data, dtype=object)
        elif (index is not None):
            value = data
            if (dtype is None):
                (dtype, value) = infer_dtype_from_scalar(value, pandas_dtype=True)
            else:
                value = maybe_cast_to_datetime(value, dtype)
            subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
        else:
            return subarr.item()
    elif (subarr.ndim == 1):
        if (index is not None):
            if ((len(subarr) != len(index)) and (len(subarr) == 1)):
                subarr = construct_1d_arraylike_from_scalar(subarr[0], len(index), subarr.dtype)
    elif (subarr.ndim > 1):
        if isinstance(data, np.ndarray):
            raise ValueError('Data must be 1-dimensional')
        else:
            subarr = com.asarray_tuplesafe(data, dtype=dtype)
    if (not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype))):
        if issubclass(subarr.dtype.type, str):
            if (not lib.is_scalar(data)):
                if (not np.all(isna(data))):
                    data = np.array(data, dtype=dtype, copy=False)
                subarr = np.array(data, dtype=object, copy=copy)
        is_object_or_str_dtype = (is_object_dtype(dtype) or is_string_dtype(dtype))
        if (is_object_dtype(subarr.dtype) and (not is_object_or_str_dtype)):
            inferred = lib.infer_dtype(subarr, skipna=False)
            if (inferred in {'interval', 'period'}):
                subarr = array(subarr)
    return subarr
Sanitize input data to an ndarray or ExtensionArray, copy if specified,
coerce to the dtype if specified.
def sanitize_array(data, index: Optional[Index], dtype: Optional[DtypeObj]=None, copy: bool=False, raise_cast_failure: bool=False) -> ArrayLike:
    '\n    Sanitize input data to an ndarray or ExtensionArray, copy if specified,\n    coerce to the dtype if specified.\n    '
    if isinstance(data, ma.MaskedArray):
        mask = ma.getmaskarray(data)
        if mask.any():
            (data, fill_value) = maybe_upcast(data, copy=True)
            data.soften_mask()
            data[mask] = fill_value
        else:
            data = data.copy()
    data = extract_array(data, extract_numpy=True)
    if isinstance(data, np.ndarray):
        if ((dtype is not None) and is_float_dtype(data.dtype) and is_integer_dtype(dtype)):
            try:
                subarr = _try_cast(data, dtype, copy, True)
            except ValueError:
                if copy:
                    subarr = data.copy()
                else:
                    subarr = np.array(data, copy=False)
        else:
            subarr = _try_cast(data, dtype, copy, raise_cast_failure)
    elif isinstance(data, ABCExtensionArray):
        subarr = data
        if (dtype is not None):
            subarr = subarr.astype(dtype, copy=copy)
        elif copy:
            subarr = subarr.copy()
        return subarr
    elif (isinstance(data, (list, tuple, abc.Set, abc.ValuesView)) and (len(data) > 0)):
        if isinstance(data, set):
            raise TypeError('Set type is unordered')
        data = list(data)
        if (dtype is not None):
            subarr = _try_cast(data, dtype, copy, raise_cast_failure)
        else:
            subarr = maybe_convert_platform(data)
        subarr = maybe_cast_to_datetime(subarr, dtype)
    elif isinstance(data, range):
        arr = np.arange(data.start, data.stop, data.step, dtype='int64')
        subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
    elif (lib.is_scalar(data) and (index is not None) and (dtype is not None)):
        data = maybe_cast_to_datetime(data, dtype)
        if (not lib.is_scalar(data)):
            data = data[0]
        subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
    else:
        subarr = _try_cast(data, dtype, copy, raise_cast_failure)
    if (getattr(subarr, 'ndim', 0) == 0):
        if isinstance(data, list):
            subarr = np.array(data, dtype=object)
        elif (index is not None):
            value = data
            if (dtype is None):
                (dtype, value) = infer_dtype_from_scalar(value, pandas_dtype=True)
            else:
                value = maybe_cast_to_datetime(value, dtype)
            subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
        else:
            return subarr.item()
    elif (subarr.ndim == 1):
        if (index is not None):
            if ((len(subarr) != len(index)) and (len(subarr) == 1)):
                subarr = construct_1d_arraylike_from_scalar(subarr[0], len(index), subarr.dtype)
    elif (subarr.ndim > 1):
        if isinstance(data, np.ndarray):
            raise ValueError('Data must be 1-dimensional')
        else:
            subarr = com.asarray_tuplesafe(data, dtype=dtype)
    if (not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype))):
        if issubclass(subarr.dtype.type, str):
            if (not lib.is_scalar(data)):
                if (not np.all(isna(data))):
                    data = np.array(data, dtype=dtype, copy=False)
                subarr = np.array(data, dtype=object, copy=copy)
        is_object_or_str_dtype = (is_object_dtype(dtype) or is_string_dtype(dtype))
        if (is_object_dtype(subarr.dtype) and (not is_object_or_str_dtype)):
            inferred = lib.infer_dtype(subarr, skipna=False)
            if (inferred in {'interval', 'period'}):
                subarr = array(subarr)
    return subarr