@@ -428,7 +428,7 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]:
428428 # Up/Down-casting
429429
430430 @final
431- def coerce_to_target_dtype (self , other , warn_on_upcast : bool = False ) -> Block :
431+ def coerce_to_target_dtype (self , other , raise_on_upcast : bool ) -> Block :
432432 """
433433 coerce the current block to a dtype compat for other
434434 we will return a block, possibly object, and not raise
@@ -455,25 +455,18 @@ def coerce_to_target_dtype(self, other, warn_on_upcast: bool = False) -> Block:
455455 isinstance (other , (np .datetime64 , np .timedelta64 )) and np .isnat (other )
456456 )
457457 ):
458- warn_on_upcast = False
458+ raise_on_upcast = False
459459 elif (
460460 isinstance (other , np .ndarray )
461461 and other .ndim == 1
462462 and is_integer_dtype (self .values .dtype )
463463 and is_float_dtype (other .dtype )
464464 and lib .has_only_ints_or_nan (other )
465465 ):
466- warn_on_upcast = False
467-
468- if warn_on_upcast :
469- warnings .warn (
470- f"Setting an item of incompatible dtype is deprecated "
471- "and will raise an error in a future version of pandas. "
472- f"Value '{ other } ' has dtype incompatible with { self .values .dtype } , "
473- "please explicitly cast to a compatible dtype first." ,
474- FutureWarning ,
475- stacklevel = find_stack_level (),
476- )
466+ raise_on_upcast = False
467+
468+ if raise_on_upcast :
469+ raise TypeError (f"Invalid value '{ other } ' for dtype '{ self .values .dtype } '" )
477470 if self .values .dtype == new_dtype :
478471 raise AssertionError (
479472 f"Did not expect new dtype { new_dtype } to equal self.dtype "
@@ -720,7 +713,7 @@ def replace(
720713 if value is None or value is NA :
721714 blk = self .astype (np .dtype (object ))
722715 else :
723- blk = self .coerce_to_target_dtype (value )
716+ blk = self .coerce_to_target_dtype (value , raise_on_upcast = False )
724717 return blk .replace (
725718 to_replace = to_replace ,
726719 value = value ,
@@ -1105,7 +1098,7 @@ def setitem(self, indexer, value) -> Block:
11051098 casted = np_can_hold_element (values .dtype , value )
11061099 except LossySetitemError :
11071100 # current dtype cannot store value, coerce to common dtype
1108- nb = self .coerce_to_target_dtype (value , warn_on_upcast = True )
1101+ nb = self .coerce_to_target_dtype (value , raise_on_upcast = True )
11091102 return nb .setitem (indexer , value )
11101103 else :
11111104 if self .dtype == _dtype_obj :
@@ -1176,7 +1169,7 @@ def putmask(self, mask, new) -> list[Block]:
11761169 if not is_list_like (new ):
11771170 # using just new[indexer] can't save us the need to cast
11781171 return self .coerce_to_target_dtype (
1179- new , warn_on_upcast = True
1172+ new , raise_on_upcast = True
11801173 ).putmask (mask , new )
11811174 else :
11821175 indexer = mask .nonzero ()[0 ]
@@ -1244,7 +1237,7 @@ def where(self, other, cond) -> list[Block]:
12441237 if self .ndim == 1 or self .shape [0 ] == 1 :
12451238 # no need to split columns
12461239
1247- block = self .coerce_to_target_dtype (other )
1240+ block = self .coerce_to_target_dtype (other , raise_on_upcast = False )
12481241 return block .where (orig_other , cond )
12491242
12501243 else :
@@ -1438,7 +1431,7 @@ def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
14381431 fill_value ,
14391432 )
14401433 except LossySetitemError :
1441- nb = self .coerce_to_target_dtype (fill_value )
1434+ nb = self .coerce_to_target_dtype (fill_value , raise_on_upcast = False )
14421435 return nb .shift (periods , fill_value = fill_value )
14431436
14441437 else :
@@ -1637,11 +1630,11 @@ def setitem(self, indexer, value):
16371630 except (ValueError , TypeError ):
16381631 if isinstance (self .dtype , IntervalDtype ):
16391632 # see TestSetitemFloatIntervalWithIntIntervalValues
1640- nb = self .coerce_to_target_dtype (orig_value , warn_on_upcast = True )
1633+ nb = self .coerce_to_target_dtype (orig_value , raise_on_upcast = True )
16411634 return nb .setitem (orig_indexer , orig_value )
16421635
16431636 elif isinstance (self , NDArrayBackedExtensionBlock ):
1644- nb = self .coerce_to_target_dtype (orig_value , warn_on_upcast = True )
1637+ nb = self .coerce_to_target_dtype (orig_value , raise_on_upcast = True )
16451638 return nb .setitem (orig_indexer , orig_value )
16461639
16471640 else :
@@ -1676,13 +1669,13 @@ def where(self, other, cond) -> list[Block]:
16761669 if self .ndim == 1 or self .shape [0 ] == 1 :
16771670 if isinstance (self .dtype , IntervalDtype ):
16781671 # TestSetitemFloatIntervalWithIntIntervalValues
1679- blk = self .coerce_to_target_dtype (orig_other )
1672+ blk = self .coerce_to_target_dtype (orig_other , raise_on_upcast = False )
16801673 return blk .where (orig_other , orig_cond )
16811674
16821675 elif isinstance (self , NDArrayBackedExtensionBlock ):
16831676 # NB: not (yet) the same as
16841677 # isinstance(values, NDArrayBackedExtensionArray)
1685- blk = self .coerce_to_target_dtype (orig_other )
1678+ blk = self .coerce_to_target_dtype (orig_other , raise_on_upcast = False )
16861679 return blk .where (orig_other , orig_cond )
16871680
16881681 else :
@@ -1737,13 +1730,13 @@ def putmask(self, mask, new) -> list[Block]:
17371730 if isinstance (self .dtype , IntervalDtype ):
17381731 # Discussion about what we want to support in the general
17391732 # case GH#39584
1740- blk = self .coerce_to_target_dtype (orig_new , warn_on_upcast = True )
1733+ blk = self .coerce_to_target_dtype (orig_new , raise_on_upcast = True )
17411734 return blk .putmask (orig_mask , orig_new )
17421735
17431736 elif isinstance (self , NDArrayBackedExtensionBlock ):
17441737 # NB: not (yet) the same as
17451738 # isinstance(values, NDArrayBackedExtensionArray)
1746- blk = self .coerce_to_target_dtype (orig_new , warn_on_upcast = True )
1739+ blk = self .coerce_to_target_dtype (orig_new , raise_on_upcast = True )
17471740 return blk .putmask (orig_mask , orig_new )
17481741
17491742 else :
0 commit comments