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,
)
| 529 | |
| 530 | |
| 531 | def 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 |