diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index b8375af797b3a..76c9d013575f0 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -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 ( @@ -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) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 44224f9709699..ad18cb8dd5bc6 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -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, @@ -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) @@ -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() diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 165e63e23d60e..fafa1f51c823e 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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 @@ -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) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index de60cda382fba..9cad404f2ce82 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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, @@ -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):