@@ -668,6 +668,14 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
668
668
def _info_axis (self ) -> Index :
669
669
return getattr (self , self ._info_axis_name )
670
670
671
+ def _is_view_after_cow_rules (self ):
672
+ # Only to be used in cases of chained assignment checks, this is a
673
+ # simplified check that assumes that either the whole object is a view
674
+ # or a copy
675
+ if len (self ._mgr .blocks ) == 0 : # type: ignore[union-attr]
676
+ return False
677
+ return self ._mgr .blocks [0 ].refs .has_reference () # type: ignore[union-attr]
678
+
671
679
@property
672
680
def shape (self ) -> tuple [int , ...]:
673
681
"""
@@ -7285,7 +7293,11 @@ def fillna(
7285
7293
ChainedAssignmentError ,
7286
7294
stacklevel = 2 ,
7287
7295
)
7288
- elif not PYPY and not using_copy_on_write ():
7296
+ elif (
7297
+ not PYPY
7298
+ and not using_copy_on_write ()
7299
+ and self ._is_view_after_cow_rules ()
7300
+ ):
7289
7301
ctr = sys .getrefcount (self )
7290
7302
ref_count = REF_COUNT
7291
7303
if isinstance (self , ABCSeries ) and _check_cacher (self ):
@@ -7567,7 +7579,11 @@ def ffill(
7567
7579
ChainedAssignmentError ,
7568
7580
stacklevel = 2 ,
7569
7581
)
7570
- elif not PYPY and not using_copy_on_write ():
7582
+ elif (
7583
+ not PYPY
7584
+ and not using_copy_on_write ()
7585
+ and self ._is_view_after_cow_rules ()
7586
+ ):
7571
7587
ctr = sys .getrefcount (self )
7572
7588
ref_count = REF_COUNT
7573
7589
if isinstance (self , ABCSeries ) and _check_cacher (self ):
@@ -7750,7 +7766,11 @@ def bfill(
7750
7766
ChainedAssignmentError ,
7751
7767
stacklevel = 2 ,
7752
7768
)
7753
- elif not PYPY and not using_copy_on_write ():
7769
+ elif (
7770
+ not PYPY
7771
+ and not using_copy_on_write ()
7772
+ and self ._is_view_after_cow_rules ()
7773
+ ):
7754
7774
ctr = sys .getrefcount (self )
7755
7775
ref_count = REF_COUNT
7756
7776
if isinstance (self , ABCSeries ) and _check_cacher (self ):
@@ -7916,7 +7936,11 @@ def replace(
7916
7936
ChainedAssignmentError ,
7917
7937
stacklevel = 2 ,
7918
7938
)
7919
- elif not PYPY and not using_copy_on_write ():
7939
+ elif (
7940
+ not PYPY
7941
+ and not using_copy_on_write ()
7942
+ and self ._is_view_after_cow_rules ()
7943
+ ):
7920
7944
ctr = sys .getrefcount (self )
7921
7945
ref_count = REF_COUNT
7922
7946
if isinstance (self , ABCSeries ) and _check_cacher (self ):
@@ -8357,7 +8381,11 @@ def interpolate(
8357
8381
ChainedAssignmentError ,
8358
8382
stacklevel = 2 ,
8359
8383
)
8360
- elif not PYPY and not using_copy_on_write ():
8384
+ elif (
8385
+ not PYPY
8386
+ and not using_copy_on_write ()
8387
+ and self ._is_view_after_cow_rules ()
8388
+ ):
8361
8389
ctr = sys .getrefcount (self )
8362
8390
ref_count = REF_COUNT
8363
8391
if isinstance (self , ABCSeries ) and _check_cacher (self ):
@@ -8995,7 +9023,11 @@ def clip(
8995
9023
ChainedAssignmentError ,
8996
9024
stacklevel = 2 ,
8997
9025
)
8998
- elif not PYPY and not using_copy_on_write ():
9026
+ elif (
9027
+ not PYPY
9028
+ and not using_copy_on_write ()
9029
+ and self ._is_view_after_cow_rules ()
9030
+ ):
8999
9031
ctr = sys .getrefcount (self )
9000
9032
ref_count = REF_COUNT
9001
9033
if isinstance (self , ABCSeries ) and hasattr (self , "_cacher" ):
@@ -10943,7 +10975,11 @@ def where(
10943
10975
ChainedAssignmentError ,
10944
10976
stacklevel = 2 ,
10945
10977
)
10946
- elif not PYPY and not using_copy_on_write ():
10978
+ elif (
10979
+ not PYPY
10980
+ and not using_copy_on_write ()
10981
+ and self ._is_view_after_cow_rules ()
10982
+ ):
10947
10983
ctr = sys .getrefcount (self )
10948
10984
ref_count = REF_COUNT
10949
10985
if isinstance (self , ABCSeries ) and hasattr (self , "_cacher" ):
@@ -11022,7 +11058,11 @@ def mask(
11022
11058
ChainedAssignmentError ,
11023
11059
stacklevel = 2 ,
11024
11060
)
11025
- elif not PYPY and not using_copy_on_write ():
11061
+ elif (
11062
+ not PYPY
11063
+ and not using_copy_on_write ()
11064
+ and self ._is_view_after_cow_rules ()
11065
+ ):
11026
11066
ctr = sys .getrefcount (self )
11027
11067
ref_count = REF_COUNT
11028
11068
if isinstance (self , ABCSeries ) and hasattr (self , "_cacher" ):
0 commit comments