def sanitize_array(data, index, dtype=None, copy=False,<EOL>raise_cast_failure=False):
if dtype is not None:<EOL><INDENT>dtype = pandas_dtype(dtype)<EOL><DEDENT>if isinstance(data, ma.MaskedArray):<EOL><INDENT>mask = ma.getmaskarray(data)<EOL>if mask.any():<EOL><INDENT>data, fill_value = maybe_upcast(data, copy=True)<EOL>data.soften_mask()  <EOL>data[mask] = fill_value<EOL><DEDENT>else:<EOL><INDENT>data = data.copy()<EOL><DEDENT><DEDENT>data = extract_array(data, extract_numpy=True)<EOL>if isinstance(data, np.ndarray):<EOL><INDENT>if dtype is not None:<EOL><INDENT>subarr = np.array(data, copy=False)<EOL>if is_float_dtype(data.dtype) and is_integer_dtype(dtype):<EOL><INDENT>try:<EOL><INDENT>subarr = _try_cast(data, True, dtype, copy,<EOL>True)<EOL><DEDENT>except ValueError:<EOL><INDENT>if copy:<EOL><INDENT>subarr = data.copy()<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>subarr = _try_cast(data, True, dtype, copy, raise_cast_failure)<EOL><DEDENT><DEDENT>elif isinstance(data, Index):<EOL><INDENT>subarr = sanitize_index(data, index, copy=copy)<EOL><DEDENT>else:<EOL><INDENT>subarr = _try_cast(data, True, dtype, copy, raise_cast_failure)<EOL><DEDENT><DEDENT>elif isinstance(data, ExtensionArray):<EOL><INDENT>if isinstance(data, ABCPandasArray):<EOL><INDENT>subarr = data.to_numpy()<EOL><DEDENT>else:<EOL><INDENT>subarr = data<EOL><DEDENT>if dtype is not None:<EOL><INDENT>subarr = data.astype(dtype)<EOL><DEDENT>if copy:<EOL><INDENT>subarr = data.copy()<EOL><DEDENT>return subarr<EOL><DEDENT>elif isinstance(data, (list, tuple)) and len(data) > <NUM_LIT:0>:<EOL><INDENT>if dtype is not None:<EOL><INDENT>try:<EOL><INDENT>subarr = _try_cast(data, False, dtype, copy,<EOL>raise_cast_failure)<EOL><DEDENT>except Exception:<EOL><INDENT>if raise_cast_failure:  <EOL><INDENT>raise<EOL><DEDENT>subarr = np.array(data, dtype=object, copy=copy)<EOL>subarr = lib.maybe_convert_objects(subarr)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>subarr = maybe_convert_platform(data)<EOL><DEDENT>subarr = maybe_cast_to_datetime(subarr, dtype)<EOL><DEDENT>elif isinstance(data, range):<EOL><INDENT>arr = np.arange(data.start, data.stop, data.step, dtype='<STR_LIT>')<EOL>subarr = _try_cast(arr, False, dtype, copy, raise_cast_failure)<EOL><DEDENT>else:<EOL><INDENT>subarr = _try_cast(data, False, dtype, copy, raise_cast_failure)<EOL><DEDENT>if getattr(subarr, '<STR_LIT>', <NUM_LIT:0>) == <NUM_LIT:0>:<EOL><INDENT>if isinstance(data, list):  <EOL><INDENT>subarr = np.array(data, dtype=object)<EOL><DEDENT>elif index is not None:<EOL><INDENT>value = data<EOL>if dtype is None:<EOL><INDENT>dtype, value = infer_dtype_from_scalar(value)<EOL><DEDENT>else:<EOL><INDENT>value = maybe_cast_to_datetime(value, dtype)<EOL><DEDENT>subarr = construct_1d_arraylike_from_scalar(<EOL>value, len(index), dtype)<EOL><DEDENT>else:<EOL><INDENT>return subarr.item()<EOL><DEDENT><DEDENT>elif subarr.ndim == <NUM_LIT:1>:<EOL><INDENT>if index is not None:<EOL><INDENT>if len(subarr) != len(index) and len(subarr) == <NUM_LIT:1>:<EOL><INDENT>subarr = construct_1d_arraylike_from_scalar(<EOL>subarr[<NUM_LIT:0>], len(index), subarr.dtype)<EOL><DEDENT><DEDENT><DEDENT>elif subarr.ndim > <NUM_LIT:1>:<EOL><INDENT>if isinstance(data, np.ndarray):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>subarr = com.asarray_tuplesafe(data, dtype=dtype)<EOL><DEDENT><DEDENT>if issubclass(subarr.dtype.type, str):<EOL><INDENT>if not lib.is_scalar(data):<EOL><INDENT>if not np.all(isna(data)):<EOL><INDENT>data = np.array(data, dtype=dtype, copy=False)<EOL><DEDENT>subarr = np.array(data, dtype=object, copy=copy)<EOL><DEDENT><DEDENT>if is_object_dtype(subarr.dtype) and dtype != '<STR_LIT:object>':<EOL><INDENT>inferred = lib.infer_dtype(subarr, skipna=False)<EOL>if inferred == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>subarr = period_array(subarr)<EOL><DEDENT>except IncompatibleFrequency:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return subarr<EOL>
Sanitize input data to an ndarray, copy if specified, coerce to the
dtype if specified.