From 68af237cf46b86b332b3e4595929305858f2cc32 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 10 Jun 2024 10:26:12 +0200 Subject: [PATCH 1/2] API/CoW: take view of underlying Block values for shallow (deep=False) copies --- pandas/core/internals/blocks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index cffb1f658a640..41d0eb37608ff 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -651,6 +651,7 @@ def copy(self, deep: bool = True) -> Self: values = values.copy() refs = None else: + values = values.view() refs = self.refs return type(self)(values, placement=self._mgr_locs, ndim=self.ndim, refs=refs) From e45e4230f9dc0ef6caa2a7711b0b42652cbb97cc Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 10 Jun 2024 12:54:11 +0200 Subject: [PATCH 2/2] use self instead of shallow copy for inplace operations --- pandas/core/internals/blocks.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 41d0eb37608ff..8fd6403bca574 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -659,9 +659,8 @@ def copy(self, deep: bool = True) -> Self: # Copy-on-Write Helpers def _maybe_copy(self, inplace: bool) -> Self: - if inplace: - deep = self.refs.has_reference() - return self.copy(deep=deep) + if inplace and not self.refs.has_reference(): + return self return self.copy() @final