Skip to content

REF: remove no-longer-needed _validate_numeric_casting #46131

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
Feb 26, 2022
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
8 changes: 1 addition & 7 deletions pandas/core/array_algos/putmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
59 changes: 0 additions & 59 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down