@@ -2575,6 +2575,22 @@ def __getitem__(self, key):
25752575 return super ().__getitem__ (key )
25762576
25772577 def __setitem__ (self , key , value ) -> None :
2578+ if not PYPY and using_copy_on_write ():
2579+ if sys .getrefcount (self .obj ) <= 2 :
2580+ warnings .warn (
2581+ _chained_assignment_msg , ChainedAssignmentError , stacklevel = 2
2582+ )
2583+ elif not PYPY and not using_copy_on_write ():
2584+ ctr = sys .getrefcount (self .obj )
2585+ ref_count = 2
2586+ if not warn_copy_on_write () and _check_cacher (self .obj ):
2587+ # see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
2588+ ref_count += 1
2589+ if ctr <= ref_count :
2590+ warnings .warn (
2591+ _chained_assignment_warning_msg , FutureWarning , stacklevel = 2
2592+ )
2593+
25782594 if self .ndim == 2 and not self ._axes_are_unique :
25792595 # GH#33041 fall back to .loc
25802596 if not isinstance (key , tuple ) or not all (is_scalar (x ) for x in key ):
@@ -2599,6 +2615,25 @@ def _convert_key(self, key):
25992615 raise ValueError ("iAt based indexing can only have integer indexers" )
26002616 return key
26012617
2618+ def __setitem__ (self , key , value ) -> None :
2619+ if not PYPY and using_copy_on_write ():
2620+ if sys .getrefcount (self .obj ) <= 2 :
2621+ warnings .warn (
2622+ _chained_assignment_msg , ChainedAssignmentError , stacklevel = 2
2623+ )
2624+ elif not PYPY and not using_copy_on_write ():
2625+ ctr = sys .getrefcount (self .obj )
2626+ ref_count = 2
2627+ if not warn_copy_on_write () and _check_cacher (self .obj ):
2628+ # see https://github.com/pandas-dev/pandas/pull/56060#discussion_r1399245221
2629+ ref_count += 1
2630+ if ctr <= ref_count :
2631+ warnings .warn (
2632+ _chained_assignment_warning_msg , FutureWarning , stacklevel = 2
2633+ )
2634+
2635+ return super ().__setitem__ (key , value )
2636+
26022637
26032638def _tuplify (ndim : int , loc : Hashable ) -> tuple [Hashable | slice , ...]:
26042639 """
0 commit comments