Skip to content

Commit 4eac590

Browse files
committed
ISSUE # 51044
Added _validate_setitem_Value
1 parent 3ad9d3f commit 4eac590

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/core/arrays/numpy_.py

+27
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,30 @@ def _wrap_ndarray_result(self, result: np.ndarray):
564564
# ------------------------------------------------------------------------
565565
# String methods interface
566566
_str_na_value = np.nan
567+
def _validate_setitem_value(self, value):
568+
"""
569+
Check if we have a scalar that we can cast losslessly.
570+
571+
Raises
572+
------
573+
TypeError
574+
"""
575+
kind = self.dtype.kind
576+
# TODO: get this all from np_can_hold_element?
577+
if kind == "b":
578+
if lib.is_bool(value):
579+
return value
580+
581+
elif kind == "f":
582+
if lib.is_integer(value) or lib.is_float(value):
583+
return value
584+
585+
else:
586+
if lib.is_integer(value) or (lib.is_float(value) and value.is_integer()):
587+
return value
588+
# TODO: unsigned checks
589+
590+
# Note: without the "str" here, the f-string rendering raises in
591+
# py38 builds.
592+
raise TypeError(f"Invalid value '{str(value)}' for dtype {self.dtype}")
593+

0 commit comments

Comments
 (0)