MCPcopy
hub / github.com/pandas-dev/pandas / sanitize_array

Function sanitize_array

pandas/core/construction.py:531–688  ·  view source on GitHub ↗

Sanitize input data to an ndarray or ExtensionArray, copy if specified, coerce to the dtype if specified. Parameters ---------- data : Any index : Index or None, default None dtype : np.dtype, ExtensionDtype, or None, default None copy : bool, default False allo

(
    data,
    index: Index | None,
    dtype: DtypeObj | None = None,
    copy: bool = False,
    *,
    allow_2d: bool = False,
)

Source from the content-addressed store, hash-verified

529
530
531def sanitize_array(
532 data,
533 index: Index | None,
534 dtype: DtypeObj | None = None,
535 copy: bool = False,
536 *,
537 allow_2d: bool = False,
538) -> ArrayLike:
539 """
540 Sanitize input data to an ndarray or ExtensionArray, copy if specified,
541 coerce to the dtype if specified.
542
543 Parameters
544 ----------
545 data : Any
546 index : Index or None, default None
547 dtype : np.dtype, ExtensionDtype, or None, default None
548 copy : bool, default False
549 allow_2d : bool, default False
550 If False, raise if we have a 2D Arraylike.
551
552 Returns
553 -------
554 np.ndarray or ExtensionArray
555 """
556 original_dtype = dtype
557 if isinstance(data, ma.MaskedArray):
558 data = sanitize_masked_array(data)
559
560 if isinstance(dtype, NumpyEADtype):
561 # Avoid ending up with a NumpyExtensionArray
562 dtype = dtype.numpy_dtype
563
564 infer_object = not isinstance(data, (ABCIndex, ABCSeries))
565
566 # extract ndarray or ExtensionArray, ensure we have no NumpyExtensionArray
567 data = extract_array(data, extract_numpy=True, extract_range=True)
568
569 if isinstance(data, np.ndarray) and data.ndim == 0:
570 if dtype is None:
571 dtype = data.dtype
572 data = lib.item_from_zerodim(data)
573 elif isinstance(data, range):
574 # GH#16804
575 data = range_to_ndarray(data)
576 copy = False
577
578 if not is_list_like(data):
579 if index is None:
580 raise ValueError("index must be specified when data is not list-like")
581 if isinstance(data, str) and using_string_dtype() and original_dtype is None:
582 from pandas.core.arrays.string_ import StringDtype
583
584 dtype = StringDtype(na_value=np.nan)
585 data = construct_1d_arraylike_from_scalar(data, len(index), dtype)
586
587 return data
588

Callers 14

__init__Method · 0.90
_sanitize_columnMethod · 0.90
arithmetic_opFunction · 0.90
comparison_opFunction · 0.90
new_methodFunction · 0.90
ndarray_to_mgrFunction · 0.90
_homogenizeFunction · 0.90
__init__Method · 0.90
__init__Method · 0.90
_reorder_indexerMethod · 0.90
__new__Method · 0.90

Calls 15

construct_array_typeMethod · 0.95
using_string_dtypeFunction · 0.90
StringDtypeClass · 0.90
maybe_convert_platformFunction · 0.90
sanitize_masked_arrayFunction · 0.85
extract_arrayFunction · 0.85
range_to_ndarrayFunction · 0.85
_sanitize_non_orderedFunction · 0.85
_try_castFunction · 0.85
_sanitize_ndimFunction · 0.85
_sanitize_str_dtypesFunction · 0.85