Skip to content

Commit cec2f5f

Browse files
authored
REF: handle non-list_like cases upfront in sanitize_array (#38563)
1 parent e68016c commit cec2f5f

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

pandas/core/construction.py

+34-22
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ def sanitize_array(
449449
# extract ndarray or ExtensionArray, ensure we have no PandasArray
450450
data = extract_array(data, extract_numpy=True)
451451

452+
if isinstance(data, np.ndarray) and data.ndim == 0:
453+
if dtype is None:
454+
dtype = data.dtype
455+
data = lib.item_from_zerodim(data)
456+
452457
# GH#846
453458
if isinstance(data, np.ndarray):
454459

@@ -462,7 +467,7 @@ def sanitize_array(
462467
else:
463468
subarr = np.array(data, copy=False)
464469
else:
465-
# we will try to copy be-definition here
470+
# we will try to copy by-definition here
466471
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
467472

468473
elif isinstance(data, ABCExtensionArray):
@@ -491,30 +496,16 @@ def sanitize_array(
491496
# GH#16804
492497
arr = np.arange(data.start, data.stop, data.step, dtype="int64")
493498
subarr = _try_cast(arr, dtype, copy, raise_cast_failure)
494-
elif lib.is_scalar(data) and index is not None and dtype is not None:
499+
500+
elif not is_list_like(data):
501+
if index is None:
502+
raise ValueError("index must be specified when data is not list-like")
495503
subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
504+
496505
else:
497506
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
498507

499-
# scalar like, GH
500-
if getattr(subarr, "ndim", 0) == 0:
501-
if isinstance(data, list): # pragma: no cover
502-
subarr = np.array(data, dtype=object)
503-
elif index is not None:
504-
subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
505-
506-
else:
507-
return subarr.item()
508-
509-
# the result that we want
510-
elif subarr.ndim == 1:
511-
subarr = _maybe_repeat(subarr, index)
512-
513-
elif subarr.ndim > 1:
514-
if isinstance(data, np.ndarray):
515-
raise ValueError("Data must be 1-dimensional")
516-
else:
517-
subarr = com.asarray_tuplesafe(data, dtype=dtype)
508+
subarr = _sanitize_ndim(subarr, data, dtype, index)
518509

519510
if not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype)):
520511
subarr = _sanitize_str_dtypes(subarr, data, dtype, copy)
@@ -528,6 +519,27 @@ def sanitize_array(
528519
return subarr
529520

530521

522+
def _sanitize_ndim(
523+
result: ArrayLike, data, dtype: Optional[DtypeObj], index: Optional[Index]
524+
) -> ArrayLike:
525+
"""
526+
Ensure we have a 1-dimensional result array.
527+
"""
528+
if getattr(result, "ndim", 0) == 0:
529+
raise ValueError("result should be arraylike with ndim > 0")
530+
531+
elif result.ndim == 1:
532+
# the result that we want
533+
result = _maybe_repeat(result, index)
534+
535+
elif result.ndim > 1:
536+
if isinstance(data, np.ndarray):
537+
raise ValueError("Data must be 1-dimensional")
538+
else:
539+
result = com.asarray_tuplesafe(data, dtype=dtype)
540+
return result
541+
542+
531543
def _sanitize_str_dtypes(
532544
result: np.ndarray, data, dtype: Optional[DtypeObj], copy: bool
533545
) -> np.ndarray:
@@ -565,7 +577,7 @@ def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bo
565577
566578
Parameters
567579
----------
568-
arr : ndarray, scalar, list, tuple, iterator (catchall)
580+
arr : ndarray, list, tuple, iterator (catchall)
569581
Excludes: ExtensionArray, Series, Index.
570582
dtype : np.dtype, ExtensionDtype or None
571583
copy : bool

pandas/core/dtypes/cast.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,10 @@ def construct_1d_arraylike_from_scalar(
15431543
"""
15441544

15451545
if dtype is None:
1546-
dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)
1546+
try:
1547+
dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)
1548+
except OutOfBoundsDatetime:
1549+
dtype = np.dtype(object)
15471550

15481551
if is_extension_array_dtype(dtype):
15491552
cls = dtype.construct_array_type()

0 commit comments

Comments
 (0)