@@ -1120,11 +1120,8 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
1120
1120
# test_iloc_setitem_custom_object
1121
1121
casted = setitem_datetimelike_compat (values , len (vi ), casted )
1122
1122
1123
- if using_cow and self .refs .has_reference ():
1124
- values = values .copy ()
1125
- self = self .make_block_same_class (
1126
- values .T if values .ndim == 2 else values
1127
- )
1123
+ self = self ._maybe_copy (using_cow , inplace = True )
1124
+ values = cast (np .ndarray , self .values .T )
1128
1125
if isinstance (casted , np .ndarray ) and casted .ndim == 1 and len (casted ) == 1 :
1129
1126
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
1130
1127
casted = casted [0 , ...]
@@ -1167,14 +1164,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
1167
1164
try :
1168
1165
casted = np_can_hold_element (values .dtype , new )
1169
1166
1170
- if using_cow and self .refs .has_reference ():
1171
- # Do this here to avoid copying twice
1172
- values = values .copy ()
1173
- self = self .make_block_same_class (values )
1167
+ self = self ._maybe_copy (using_cow , inplace = True )
1168
+ values = cast (np .ndarray , self .values )
1174
1169
1175
1170
putmask_without_repeat (values .T , mask , casted )
1176
- if using_cow :
1177
- return [self .copy (deep = False )]
1178
1171
return [self ]
1179
1172
except LossySetitemError :
1180
1173
if self .ndim == 1 or self .shape [0 ] == 1 :
@@ -1808,10 +1801,6 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
1808
1801
if new is lib .no_default :
1809
1802
new = self .fill_value
1810
1803
1811
- values = self .values
1812
- if values .ndim == 2 :
1813
- values = values .T
1814
-
1815
1804
orig_new = new
1816
1805
orig_mask = mask
1817
1806
new = self ._maybe_squeeze_arg (new )
@@ -1822,9 +1811,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
1822
1811
return [self .copy (deep = False )]
1823
1812
return [self ]
1824
1813
1825
- if using_cow and self .refs .has_reference ():
1826
- values = values .copy ()
1827
- self = self .make_block_same_class (values .T if values .ndim == 2 else values )
1814
+ self = self ._maybe_copy (using_cow , inplace = True )
1815
+ values = self .values
1816
+ if values .ndim == 2 :
1817
+ values = values .T
1828
1818
1829
1819
try :
1830
1820
# Caller is responsible for ensuring matching lengths
0 commit comments