pandas/core/construction.py
def sanitize_array(
    data,
    index: Optional[Index],
    dtype: Optional[DtypeObj] = None,
    copy: bool = False,
    raise_cast_failure: bool = False,
) -> ArrayLike:
    """
    Sanitize input data to an ndarray or ExtensionArray, copy if specified,
    coerce to the dtype if specified.
    """

    if isinstance(data, ma.MaskedArray):
        data = sanitize_masked_array(data)

    # extract ndarray or ExtensionArray, ensure we have no PandasArray
    data = extract_array(data, extract_numpy=True)

    if isinstance(data, np.ndarray) and data.ndim == 0:
        if dtype is None:
            dtype = data.dtype
        data = lib.item_from_zerodim(data)

    # GH#846
    if isinstance(data, np.ndarray):

        if dtype is not None and is_float_dtype(data.dtype) and is_integer_dtype(dtype):
            # possibility of nan -> garbage
            try:
                subarr = _try_cast(data, dtype, copy, True)
            except ValueError:
                subarr = np.array(data, copy=copy)
        else:
            # we will try to copy by-definition here
            subarr = _try_cast(data, dtype, copy, raise_cast_failure)

    elif isinstance(data, ABCExtensionArray):
        # it is already ensured above this is not a PandasArray
        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:
        # TODO: deque, array.array
        if isinstance(data, set):
            # Raise only for unordered sets, e.g., not for dict_keys
            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):
        # GH#16804
        arr = np.arange(data.start, data.stop, data.step, dtype="int64")
        subarr = _try_cast(arr, dtype, copy, raise_cast_failure)

    elif not is_list_like(data):
        if index is None:
            raise ValueError("index must be specified when data is not list-like")
        subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)

    else:
        subarr = _try_cast(data, dtype, copy, raise_cast_failure)

    subarr = _sanitize_ndim(subarr, data, dtype, index)

    if not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype)):
        subarr = _sanitize_str_dtypes(subarr, data, dtype, 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.
    
Sanitize input data to an ndarray or ExtensionArray, copy if specified,
coerce to the dtype if specified.
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:
    if isinstance(data, ma.MaskedArray):
        data = sanitize_masked_array(data)
    data = extract_array(data, extract_numpy=True)
    if isinstance(data, np.ndarray) and data.ndim == 0:
        if dtype is None:
            dtype = data.dtype
        data = lib.item_from_zerodim(data)
    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:
                subarr = np.array(data, copy=copy)
        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 not is_list_like(data):
        if index is None:
            raise ValueError("index must be specified when data is not list-like")
        subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
    else:
        subarr = _try_cast(data, dtype, copy, raise_cast_failure)
    subarr = _sanitize_ndim(subarr, data, dtype, index)
    if not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype)):
        subarr = _sanitize_str_dtypes(subarr, data, dtype, 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
def sanitize_array ( data , index : Optional [ Index ] , dtype : Optional [ DtypeObj ] = None , copy : bool = False , raise_cast_failure : bool = False , ) -> ArrayLike : if isinstance ( data , ma . MaskedArray ) : data = sanitize_masked_array ( data ) data = extract_array ( data , extract_numpy = True ) if isinstance ( data , np . ndarray ) and data . ndim == 0 : if dtype is None : dtype = data . dtype data = lib . item_from_zerodim ( data ) 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 : subarr = np . array ( data , copy = copy ) 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 not is_list_like ( data ) : if index is None : raise ValueError ( "index must be specified when data is not list-like" ) subarr = construct_1d_arraylike_from_scalar ( data , len ( index ) , dtype ) else : subarr = _try_cast ( data , dtype , copy , raise_cast_failure ) subarr = _sanitize_ndim ( subarr , data , dtype , index ) if not ( is_extension_array_dtype ( subarr . dtype ) or is_extension_array_dtype ( dtype ) ) : subarr = _sanitize_str_dtypes ( subarr , data , dtype , 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.
Sanitize input data to an ndarray or ExtensionArray copy if specified coerce to the dtype if specified .
"""
    Sanitize input data to an ndarray or ExtensionArray, copy if specified,
    coerce to the dtype if specified.
    """ # extract ndarray or ExtensionArray, ensure we have no PandasArray # GH#846 # possibility of nan -> garbage # we will try to copy by-definition here # it is already ensured above this is not a PandasArray # TODO: deque, array.array # Raise only for unordered sets, e.g., not for dict_keys # GH#16804
data
index
Optional[Index]
dtype
Optional[DtypeObj]
copy
bool
raise_cast_failure
bool