Skip to content

REF: simplify casting #38404

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 3 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
astype_nansafe,
construct_1d_arraylike_from_scalar,
find_common_type,
infer_dtype_from_scalar,
maybe_box_datetimelike,
)
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -328,8 +327,8 @@ def __init__(
else:
npoints = sparse_index.length

dtype = infer_dtype_from_scalar(data)[0]
data = construct_1d_arraylike_from_scalar(data, npoints, dtype)
data = construct_1d_arraylike_from_scalar(data, npoints, dtype=None)
dtype = data.dtype

if dtype is not None:
dtype = pandas_dtype(dtype)
Expand Down
18 changes: 2 additions & 16 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
construct_1d_arraylike_from_scalar,
construct_1d_ndarray_preserving_na,
construct_1d_object_array_from_listlike,
infer_dtype_from_scalar,
maybe_cast_to_datetime,
maybe_cast_to_integer_array,
maybe_castable,
Expand Down Expand Up @@ -480,17 +479,13 @@ def sanitize_array(
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
else:
subarr = maybe_convert_platform(data)

subarr = maybe_cast_to_datetime(subarr, dtype)
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 lib.is_scalar(data) and index is not None and dtype is not None:
data = maybe_cast_to_datetime(data, dtype)
if not lib.is_scalar(data):
data = data[0]
subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)
else:
subarr = _try_cast(data, dtype, copy, raise_cast_failure)
Expand All @@ -500,16 +495,7 @@ def sanitize_array(
if isinstance(data, list): # pragma: no cover
subarr = np.array(data, dtype=object)
elif index is not None:
value = data

# figure out the dtype from the value (upcast if necessary)
if dtype is None:
dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)
else:
# need to possibly convert the value here
value = maybe_cast_to_datetime(value, dtype)

subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
subarr = construct_1d_arraylike_from_scalar(data, len(index), dtype)

else:
return subarr.item()
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ def find_common_type(types: List[DtypeObj]) -> DtypeObj:


def construct_1d_arraylike_from_scalar(
value: Scalar, length: int, dtype: DtypeObj
value: Scalar, length: int, dtype: Optional[DtypeObj]
) -> ArrayLike:
"""
create a np.ndarray / pandas type of specified shape and dtype
Expand All @@ -1583,6 +1583,10 @@ def construct_1d_arraylike_from_scalar(
np.ndarray / pandas type of length, filled with value

"""

if dtype is None:
dtype, value = infer_dtype_from_scalar(value, pandas_dtype=True)

if is_extension_array_dtype(dtype):
cls = dtype.construct_array_type()
subarr = cls._from_sequence([value] * length, dtype=dtype)
Expand Down
10 changes: 1 addition & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
infer_dtype_from_scalar,
invalidate_string_dtypes,
maybe_box_datetimelike,
maybe_cast_to_datetime,
maybe_casted_values,
maybe_convert_platform,
maybe_downcast_to_dtype,
Expand Down Expand Up @@ -3942,14 +3941,7 @@ def reindexer(value):
value = maybe_infer_to_datetimelike(value)

else:
# cast ignores pandas dtypes. so save the dtype first
infer_dtype, fill_value = infer_dtype_from_scalar(value, pandas_dtype=True)

value = construct_1d_arraylike_from_scalar(
fill_value, len(self), infer_dtype
)

value = maybe_cast_to_datetime(value, infer_dtype)
value = construct_1d_arraylike_from_scalar(value, len(self), dtype=None)

# return internal types directly
if is_extension_array_dtype(value):
Expand Down