@@ -1071,7 +1071,9 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
1071
1071
res_blocks .extend (rbs )
1072
1072
return res_blocks
1073
1073
1074
- def where (self , other , cond , _downcast : str | bool = "infer" ) -> list [Block ]:
1074
+ def where (
1075
+ self , other , cond , _downcast : str | bool = "infer" , using_cow : bool = False
1076
+ ) -> list [Block ]:
1075
1077
"""
1076
1078
evaluate the block; return result block(s) from the result
1077
1079
@@ -1102,6 +1104,8 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1102
1104
icond , noop = validate_putmask (values , ~ cond )
1103
1105
if noop :
1104
1106
# GH-39595: Always return a copy; short-circuit up/downcasting
1107
+ if using_cow :
1108
+ return [self .copy (deep = False )]
1105
1109
return [self .copy ()]
1106
1110
1107
1111
if other is lib .no_default :
@@ -1121,8 +1125,10 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1121
1125
# no need to split columns
1122
1126
1123
1127
block = self .coerce_to_target_dtype (other )
1124
- blocks = block .where (orig_other , cond )
1125
- return self ._maybe_downcast (blocks , downcast = _downcast )
1128
+ blocks = block .where (orig_other , cond , using_cow = using_cow )
1129
+ return self ._maybe_downcast (
1130
+ blocks , downcast = _downcast , using_cow = using_cow
1131
+ )
1126
1132
1127
1133
else :
1128
1134
# since _maybe_downcast would split blocks anyway, we
@@ -1139,7 +1145,9 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1139
1145
oth = other [:, i : i + 1 ]
1140
1146
1141
1147
submask = cond [:, i : i + 1 ]
1142
- rbs = nb .where (oth , submask , _downcast = _downcast )
1148
+ rbs = nb .where (
1149
+ oth , submask , _downcast = _downcast , using_cow = using_cow
1150
+ )
1143
1151
res_blocks .extend (rbs )
1144
1152
return res_blocks
1145
1153
@@ -1528,7 +1536,9 @@ def setitem(self, indexer, value, using_cow: bool = False):
1528
1536
else :
1529
1537
return self
1530
1538
1531
- def where (self , other , cond , _downcast : str | bool = "infer" ) -> list [Block ]:
1539
+ def where (
1540
+ self , other , cond , _downcast : str | bool = "infer" , using_cow : bool = False
1541
+ ) -> list [Block ]:
1532
1542
# _downcast private bc we only specify it when calling from fillna
1533
1543
arr = self .values .T
1534
1544
@@ -1546,6 +1556,8 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1546
1556
if noop :
1547
1557
# GH#44181, GH#45135
1548
1558
# Avoid a) raising for Interval/PeriodDtype and b) unnecessary object upcast
1559
+ if using_cow :
1560
+ return [self .copy (deep = False )]
1549
1561
return [self .copy ()]
1550
1562
1551
1563
try :
@@ -1557,15 +1569,19 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1557
1569
if is_interval_dtype (self .dtype ):
1558
1570
# TestSetitemFloatIntervalWithIntIntervalValues
1559
1571
blk = self .coerce_to_target_dtype (orig_other )
1560
- nbs = blk .where (orig_other , orig_cond )
1561
- return self ._maybe_downcast (nbs , downcast = _downcast )
1572
+ nbs = blk .where (orig_other , orig_cond , using_cow = using_cow )
1573
+ return self ._maybe_downcast (
1574
+ nbs , downcast = _downcast , using_cow = using_cow
1575
+ )
1562
1576
1563
1577
elif isinstance (self , NDArrayBackedExtensionBlock ):
1564
1578
# NB: not (yet) the same as
1565
1579
# isinstance(values, NDArrayBackedExtensionArray)
1566
1580
blk = self .coerce_to_target_dtype (orig_other )
1567
- nbs = blk .where (orig_other , orig_cond )
1568
- return self ._maybe_downcast (nbs , downcast = _downcast )
1581
+ nbs = blk .where (orig_other , orig_cond , using_cow = using_cow )
1582
+ return self ._maybe_downcast (
1583
+ nbs , downcast = _downcast , using_cow = using_cow
1584
+ )
1569
1585
1570
1586
else :
1571
1587
raise
@@ -1583,7 +1599,7 @@ def where(self, other, cond, _downcast: str | bool = "infer") -> list[Block]:
1583
1599
n = orig_other [:, i : i + 1 ]
1584
1600
1585
1601
submask = orig_cond [:, i : i + 1 ]
1586
- rbs = nb .where (n , submask )
1602
+ rbs = nb .where (n , submask , using_cow = using_cow )
1587
1603
res_blocks .extend (rbs )
1588
1604
return res_blocks
1589
1605
0 commit comments