@@ -428,7 +428,7 @@ def split_and_operate(self, func, *args, **kwargs) -> list[Block]:
428
428
# Up/Down-casting
429
429
430
430
@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 :
432
432
"""
433
433
coerce the current block to a dtype compat for other
434
434
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:
455
455
isinstance (other , (np .datetime64 , np .timedelta64 )) and np .isnat (other )
456
456
)
457
457
):
458
- warn_on_upcast = False
458
+ raise_on_upcast = False
459
459
elif (
460
460
isinstance (other , np .ndarray )
461
461
and other .ndim == 1
462
462
and is_integer_dtype (self .values .dtype )
463
463
and is_float_dtype (other .dtype )
464
464
and lib .has_only_ints_or_nan (other )
465
465
):
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 } '" )
477
470
if self .values .dtype == new_dtype :
478
471
raise AssertionError (
479
472
f"Did not expect new dtype { new_dtype } to equal self.dtype "
@@ -720,7 +713,7 @@ def replace(
720
713
if value is None or value is NA :
721
714
blk = self .astype (np .dtype (object ))
722
715
else :
723
- blk = self .coerce_to_target_dtype (value )
716
+ blk = self .coerce_to_target_dtype (value , raise_on_upcast = False )
724
717
return blk .replace (
725
718
to_replace = to_replace ,
726
719
value = value ,
@@ -1105,7 +1098,7 @@ def setitem(self, indexer, value) -> Block:
1105
1098
casted = np_can_hold_element (values .dtype , value )
1106
1099
except LossySetitemError :
1107
1100
# 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 )
1109
1102
return nb .setitem (indexer , value )
1110
1103
else :
1111
1104
if self .dtype == _dtype_obj :
@@ -1176,7 +1169,7 @@ def putmask(self, mask, new) -> list[Block]:
1176
1169
if not is_list_like (new ):
1177
1170
# using just new[indexer] can't save us the need to cast
1178
1171
return self .coerce_to_target_dtype (
1179
- new , warn_on_upcast = True
1172
+ new , raise_on_upcast = True
1180
1173
).putmask (mask , new )
1181
1174
else :
1182
1175
indexer = mask .nonzero ()[0 ]
@@ -1244,7 +1237,7 @@ def where(self, other, cond) -> list[Block]:
1244
1237
if self .ndim == 1 or self .shape [0 ] == 1 :
1245
1238
# no need to split columns
1246
1239
1247
- block = self .coerce_to_target_dtype (other )
1240
+ block = self .coerce_to_target_dtype (other , raise_on_upcast = False )
1248
1241
return block .where (orig_other , cond )
1249
1242
1250
1243
else :
@@ -1438,7 +1431,7 @@ def shift(self, periods: int, fill_value: Any = None) -> list[Block]:
1438
1431
fill_value ,
1439
1432
)
1440
1433
except LossySetitemError :
1441
- nb = self .coerce_to_target_dtype (fill_value )
1434
+ nb = self .coerce_to_target_dtype (fill_value , raise_on_upcast = False )
1442
1435
return nb .shift (periods , fill_value = fill_value )
1443
1436
1444
1437
else :
@@ -1637,11 +1630,11 @@ def setitem(self, indexer, value):
1637
1630
except (ValueError , TypeError ):
1638
1631
if isinstance (self .dtype , IntervalDtype ):
1639
1632
# 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 )
1641
1634
return nb .setitem (orig_indexer , orig_value )
1642
1635
1643
1636
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 )
1645
1638
return nb .setitem (orig_indexer , orig_value )
1646
1639
1647
1640
else :
@@ -1676,13 +1669,13 @@ def where(self, other, cond) -> list[Block]:
1676
1669
if self .ndim == 1 or self .shape [0 ] == 1 :
1677
1670
if isinstance (self .dtype , IntervalDtype ):
1678
1671
# TestSetitemFloatIntervalWithIntIntervalValues
1679
- blk = self .coerce_to_target_dtype (orig_other )
1672
+ blk = self .coerce_to_target_dtype (orig_other , raise_on_upcast = False )
1680
1673
return blk .where (orig_other , orig_cond )
1681
1674
1682
1675
elif isinstance (self , NDArrayBackedExtensionBlock ):
1683
1676
# NB: not (yet) the same as
1684
1677
# 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 )
1686
1679
return blk .where (orig_other , orig_cond )
1687
1680
1688
1681
else :
@@ -1737,13 +1730,13 @@ def putmask(self, mask, new) -> list[Block]:
1737
1730
if isinstance (self .dtype , IntervalDtype ):
1738
1731
# Discussion about what we want to support in the general
1739
1732
# 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 )
1741
1734
return blk .putmask (orig_mask , orig_new )
1742
1735
1743
1736
elif isinstance (self , NDArrayBackedExtensionBlock ):
1744
1737
# NB: not (yet) the same as
1745
1738
# 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 )
1747
1740
return blk .putmask (orig_mask , orig_new )
1748
1741
1749
1742
else :
0 commit comments