From 99ce776090661be60ec48c3f0735623f080df2aa Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 28 Nov 2023 00:58:43 +0100 Subject: [PATCH] CoW: Avoid warning for ArrowDtypes when setting inplace --- pandas/core/internals/managers.py | 20 ++++++++++++++------ pandas/tests/copy_view/test_interp_fillna.py | 5 +++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 843441e4865c7..8fce2be8e4e44 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -54,7 +54,11 @@ ) import pandas.core.algorithms as algos -from pandas.core.arrays import DatetimeArray +from pandas.core.arrays import ( + ArrowExtensionArray, + ArrowStringArray, + DatetimeArray, +) from pandas.core.arrays._mixins import NDArrayBackedExtensionArray from pandas.core.construction import ( ensure_wrapped_if_datetimelike, @@ -1343,11 +1347,15 @@ def column_setitem( intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`) """ if warn_copy_on_write() and not self._has_no_reference(loc): - warnings.warn( - COW_WARNING_GENERAL_MSG, - FutureWarning, - stacklevel=find_stack_level(), - ) + if not isinstance( + self.blocks[self.blknos[loc]].values, + (ArrowExtensionArray, ArrowStringArray), + ): + warnings.warn( + COW_WARNING_GENERAL_MSG, + FutureWarning, + stacklevel=find_stack_level(), + ) elif using_copy_on_write() and not self._has_no_reference(loc): blkno = self.blknos[loc] # Split blocks to only copy the column we want to modify diff --git a/pandas/tests/copy_view/test_interp_fillna.py b/pandas/tests/copy_view/test_interp_fillna.py index cdb06de90a568..c2482645b072e 100644 --- a/pandas/tests/copy_view/test_interp_fillna.py +++ b/pandas/tests/copy_view/test_interp_fillna.py @@ -344,8 +344,9 @@ def test_fillna_inplace_ea_noop_shares_memory( assert not df._mgr._has_no_reference(1) assert not view._mgr._has_no_reference(1) - # TODO(CoW-warn) should this warn for ArrowDtype? - with tm.assert_cow_warning(warn_copy_on_write): + with tm.assert_cow_warning( + warn_copy_on_write and "pyarrow" not in any_numeric_ea_and_arrow_dtype + ): df.iloc[0, 1] = 100 if isinstance(df["a"].dtype, ArrowDtype) or using_copy_on_write: tm.assert_frame_equal(df_orig, view)