diff --git a/pandas/core/array_algos/putmask.py b/pandas/core/array_algos/putmask.py index d0779b48ae094..d2e6b6e935ed5 100644 --- a/pandas/core/array_algos/putmask.py +++ b/pandas/core/array_algos/putmask.py @@ -14,10 +14,7 @@ ) from pandas.compat import np_version_under1p20 -from pandas.core.dtypes.cast import ( - convert_scalar_for_putitemlike, - infer_dtype_from, -) +from pandas.core.dtypes.cast import infer_dtype_from from pandas.core.dtypes.common import is_list_like from pandas.core.arrays import ExtensionArray @@ -36,9 +33,6 @@ def putmask_inplace(values: ArrayLike, mask: npt.NDArray[np.bool_], value: Any) value : Any """ - if lib.is_scalar(value) and isinstance(values, np.ndarray): - value = convert_scalar_for_putitemlike(value, values.dtype) - if ( not isinstance(values, np.ndarray) or (values.dtype == object and not lib.is_scalar(value)) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 835c0a90c309d..9f25dd90caefd 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1849,65 +1849,6 @@ def maybe_cast_to_integer_array( raise ValueError(f"values cannot be losslessly cast to {dtype}") -def convert_scalar_for_putitemlike(scalar: Scalar, dtype: np.dtype) -> Scalar: - """ - Convert datetimelike scalar if we are setting into a datetime64 - or timedelta64 ndarray. - - Parameters - ---------- - scalar : scalar - dtype : np.dtype - - Returns - ------- - scalar - """ - if dtype.kind in ["m", "M"]: - scalar = maybe_box_datetimelike(scalar, dtype) - return _maybe_unbox_datetimelike(scalar, dtype) - else: - _validate_numeric_casting(dtype, scalar) - return scalar - - -def _validate_numeric_casting(dtype: np.dtype, value: Scalar) -> None: - """ - Check that we can losslessly insert the given value into an array - with the given dtype. - - Parameters - ---------- - dtype : np.dtype - value : scalar - - Raises - ------ - ValueError - """ - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "Union[Union[str, int, float, bool], Union[Any, Timestamp, Timedelta, Any]]"; - # expected "Union[Union[int, float, complex, str, bytes, generic], - # Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - if ( - issubclass(dtype.type, (np.integer, np.bool_)) - and is_float(value) - and np.isnan(value) # type: ignore[arg-type] - ): - raise ValueError("Cannot assign nan to integer series") - - elif dtype.kind in ["i", "u", "f", "c"]: - if is_bool(value) or isinstance(value, np.timedelta64): - # numpy will cast td64 to integer if we're not careful - raise ValueError( - f"Cannot assign {type(value).__name__} to float/integer series" - ) - elif dtype.kind == "b": - if is_scalar(value) and not is_bool(value): - raise ValueError(f"Cannot assign {type(value).__name__} to bool series") - - def can_hold_element(arr: ArrayLike, element: Any) -> bool: """ Can we do an inplace setitem with this element in an array with this dtype?