Skip to content

REF: helpers for sanitize_array #38553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,7 @@ def sanitize_array(

# the result that we want
elif subarr.ndim == 1:
if index is not None:

# a 1-element ndarray
if len(subarr) != len(index) and len(subarr) == 1:
subarr = subarr.repeat(len(index))
subarr = _maybe_repeat(subarr, index)

elif subarr.ndim > 1:
if isinstance(data, np.ndarray):
Expand All @@ -521,16 +517,7 @@ def sanitize_array(
subarr = com.asarray_tuplesafe(data, dtype=dtype)

if not (is_extension_array_dtype(subarr.dtype) or is_extension_array_dtype(dtype)):
# This is to prevent mixed-type Series getting all casted to
# NumPy string type, e.g. NaN --> '-1#IND'.
if issubclass(subarr.dtype.type, str):
# GH#16605
# If not empty convert the data to dtype
# GH#19853: If data is a scalar, subarr has already the result
if not lib.is_scalar(data):
if not np.all(isna(data)):
data = np.array(data, dtype=dtype, copy=False)
subarr = np.array(data, dtype=object, copy=copy)
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:
Expand All @@ -541,6 +528,37 @@ def sanitize_array(
return subarr


def _sanitize_str_dtypes(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intent is to try to deduplicate with other things we have in core/dtypes/cast ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partially. more immediately i'm focused on trying to isolate scalar cases so we can rule them out elsewhere

result: np.ndarray, data, dtype: Optional[DtypeObj], copy: bool
) -> np.ndarray:
"""
Ensure we have a dtype that is supported by pandas.
"""

# This is to prevent mixed-type Series getting all casted to
# NumPy string type, e.g. NaN --> '-1#IND'.
if issubclass(result.dtype.type, str):
# GH#16605
# If not empty convert the data to dtype
# GH#19853: If data is a scalar, result has already the result
if not lib.is_scalar(data):
if not np.all(isna(data)):
data = np.array(data, dtype=dtype, copy=False)
result = np.array(data, dtype=object, copy=copy)
return result


def _maybe_repeat(arr: ArrayLike, index: Optional[Index]) -> ArrayLike:
"""
If we have a length-1 array and an index describing how long we expect
the result to be, repeat the array.
"""
if index is not None:
if 1 == len(arr) != len(index):
arr = arr.repeat(len(index))
return arr


def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bool):
"""
Convert input to numpy ndarray and optionally cast to a given dtype.
Expand Down