Skip to content

Commit 3d7a44a

Browse files
authored
REF: use CoW helpers more (#53911)
1 parent 9372d21 commit 3d7a44a

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

pandas/core/internals/blocks.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,8 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
11201120
# test_iloc_setitem_custom_object
11211121
casted = setitem_datetimelike_compat(values, len(vi), casted)
11221122

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)
11281125
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
11291126
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
11301127
casted = casted[0, ...]
@@ -1167,14 +1164,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
11671164
try:
11681165
casted = np_can_hold_element(values.dtype, new)
11691166

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)
11741169

11751170
putmask_without_repeat(values.T, mask, casted)
1176-
if using_cow:
1177-
return [self.copy(deep=False)]
11781171
return [self]
11791172
except LossySetitemError:
11801173
if self.ndim == 1 or self.shape[0] == 1:
@@ -1808,10 +1801,6 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
18081801
if new is lib.no_default:
18091802
new = self.fill_value
18101803

1811-
values = self.values
1812-
if values.ndim == 2:
1813-
values = values.T
1814-
18151804
orig_new = new
18161805
orig_mask = mask
18171806
new = self._maybe_squeeze_arg(new)
@@ -1822,9 +1811,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
18221811
return [self.copy(deep=False)]
18231812
return [self]
18241813

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
18281818

18291819
try:
18301820
# Caller is responsible for ensuring matching lengths

0 commit comments

Comments
 (0)