Skip to content

REF: use CoW helpers more #53911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,11 +1083,8 @@ def setitem(self, indexer, value, using_cow: bool = False) -> Block:
# test_iloc_setitem_custom_object
casted = setitem_datetimelike_compat(values, len(vi), casted)

if using_cow and self.refs.has_reference():
values = values.copy()
self = self.make_block_same_class(
values.T if values.ndim == 2 else values
)
self = self._maybe_copy(using_cow, inplace=True)
values = cast(np.ndarray, self.values.T)
if isinstance(casted, np.ndarray) and casted.ndim == 1 and len(casted) == 1:
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
casted = casted[0, ...]
Expand Down Expand Up @@ -1130,14 +1127,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
try:
casted = np_can_hold_element(values.dtype, new)

if using_cow and self.refs.has_reference():
# Do this here to avoid copying twice
values = values.copy()
self = self.make_block_same_class(values)
self = self._maybe_copy(using_cow, inplace=True)
values = cast(np.ndarray, self.values)

putmask_without_repeat(values.T, mask, casted)
if using_cow:
return [self.copy(deep=False)]
return [self]
except LossySetitemError:
if self.ndim == 1 or self.shape[0] == 1:
Expand Down Expand Up @@ -1784,10 +1777,6 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
if new is lib.no_default:
new = self.fill_value

values = self.values
if values.ndim == 2:
values = values.T

orig_new = new
orig_mask = mask
new = self._maybe_squeeze_arg(new)
Expand All @@ -1798,9 +1787,10 @@ def putmask(self, mask, new, using_cow: bool = False) -> list[Block]:
return [self.copy(deep=False)]
return [self]

if using_cow and self.refs.has_reference():
values = values.copy()
self = self.make_block_same_class(values.T if values.ndim == 2 else values)
self = self._maybe_copy(using_cow, inplace=True)
values = self.values
if values.ndim == 2:
values = values.T

try:
# Caller is responsible for ensuring matching lengths
Expand Down