Skip to content

Commit 6dbc455

Browse files
authored
CoW: Set refs properly for non CoW mode (#54118)
1 parent c539d1f commit 6dbc455

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,7 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self
802802

803803
new_axes = [self._get_axis(mapping.get(k, k)) for k in range(self._AXIS_LEN)]
804804
new_values = self._values.swapaxes(i, j) # type: ignore[union-attr]
805-
if (
806-
using_copy_on_write()
807-
and self._mgr.is_single_block
808-
and isinstance(self._mgr, BlockManager)
809-
):
805+
if self._mgr.is_single_block and isinstance(self._mgr, BlockManager):
810806
# This should only get hit in case of having a single block, otherwise a
811807
# copy is made, we don't have to set up references.
812808
new_mgr = ndarray_to_mgr(
@@ -823,10 +819,10 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self
823819
new_mgr.blocks[0].refs.add_reference(
824820
new_mgr.blocks[0] # type: ignore[arg-type]
825821
)
826-
return self._constructor(new_mgr).__finalize__(self, method="swapaxes")
822+
if not using_copy_on_write() and copy is not False:
823+
new_mgr = new_mgr.copy(deep=True)
827824

828-
elif (copy or copy is None) and self._mgr.is_single_block:
829-
new_values = new_values.copy()
825+
return self._constructor(new_mgr).__finalize__(self, method="swapaxes")
830826

831827
return self._constructor(
832828
new_values,

pandas/core/internals/blocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def _downcast_2d(self, dtype, using_cow: bool = False) -> list[Block]:
486486
"""
487487
new_values = maybe_downcast_to_dtype(self.values, dtype=dtype)
488488
new_values = maybe_coerce_values(new_values)
489-
refs = self.refs if using_cow and new_values is self.values else None
489+
refs = self.refs if new_values is self.values else None
490490
return [self.make_block(new_values, refs=refs)]
491491

492492
@final
@@ -529,7 +529,7 @@ def convert(
529529
refs = None
530530
if copy and res_values is values:
531531
res_values = values.copy()
532-
elif res_values is values and using_cow:
532+
elif res_values is values:
533533
refs = self.refs
534534

535535
res_values = ensure_block_shape(res_values, self.ndim)
@@ -577,7 +577,7 @@ def astype(
577577
new_values = maybe_coerce_values(new_values)
578578

579579
refs = None
580-
if using_cow and astype_is_view(values.dtype, new_values.dtype):
580+
if (using_cow or not copy) and astype_is_view(values.dtype, new_values.dtype):
581581
refs = self.refs
582582

583583
newb = self.make_block(new_values, refs=refs)
@@ -914,7 +914,7 @@ def _replace_coerce(
914914
nb = self.astype(np.dtype(object), copy=False, using_cow=using_cow)
915915
if (nb is self or using_cow) and not inplace:
916916
nb = nb.copy()
917-
elif inplace and has_ref and nb.refs.has_reference():
917+
elif inplace and has_ref and nb.refs.has_reference() and using_cow:
918918
# no copy in astype and we had refs before
919919
nb = nb.copy()
920920
putmask_inplace(nb.values, mask, value)

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ def view(self, dtype: Dtype | None = None) -> Series:
893893
# implementation
894894
res_values = self.array.view(dtype)
895895
res_ser = self._constructor(res_values, index=self.index, copy=False)
896-
if isinstance(res_ser._mgr, SingleBlockManager) and using_copy_on_write():
896+
if isinstance(res_ser._mgr, SingleBlockManager):
897897
blk = res_ser._mgr._block
898898
blk.refs = cast("BlockValuesRefs", self._references)
899899
blk.refs.add_reference(blk) # type: ignore[arg-type]

0 commit comments

Comments
 (0)