File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -7797,6 +7797,22 @@ def replace(
7797
7797
ChainedAssignmentError ,
7798
7798
stacklevel = 2 ,
7799
7799
)
7800
+ elif not PYPY and not using_copy_on_write ():
7801
+ ctr = sys .getrefcount (self )
7802
+ ref_count = REF_COUNT
7803
+ if isinstance (self , ABCSeries ) and hasattr (self , "_cacher" ):
7804
+ # in non-CoW mode, chained Series access will populate the
7805
+ # `_item_cache` which results in an increased ref count not below
7806
+ # the threshold, while we still need to warn. We detect this case
7807
+ # of a Series derived from a DataFrame through the presence of
7808
+ # `_cacher`
7809
+ ref_count += 1
7810
+ if ctr <= ref_count :
7811
+ warnings .warn (
7812
+ _chained_assignment_warning_method_msg ,
7813
+ FutureWarning ,
7814
+ stacklevel = 2 ,
7815
+ )
7800
7816
7801
7817
if not is_bool (regex ) and to_replace is not None :
7802
7818
raise ValueError ("'to_replace' must be 'None' if 'regex' is not a bool" )
Original file line number Diff line number Diff line change 4
4
from pandas import (
5
5
Categorical ,
6
6
DataFrame ,
7
+ option_context ,
7
8
)
8
9
import pandas ._testing as tm
9
10
from pandas .tests .copy_view .util import get_array
@@ -395,6 +396,17 @@ def test_replace_chained_assignment(using_copy_on_write):
395
396
with tm .raises_chained_assignment_error ():
396
397
df [["a" ]].replace (1 , 100 , inplace = True )
397
398
tm .assert_frame_equal (df , df_orig )
399
+ else :
400
+ with tm .assert_produces_warning (FutureWarning , match = "inplace method" ):
401
+ with option_context ("mode.chained_assignment" , None ):
402
+ df [["a" ]].replace (1 , 100 , inplace = True )
403
+
404
+ with tm .assert_produces_warning (FutureWarning , match = "inplace method" ):
405
+ with option_context ("mode.chained_assignment" , None ):
406
+ df [df .a > 5 ].replace (1 , 100 , inplace = True )
407
+
408
+ with tm .assert_produces_warning (FutureWarning , match = "inplace method" ):
409
+ df ["a" ].replace (1 , 100 , inplace = True )
398
410
399
411
400
412
def test_replace_listlike (using_copy_on_write ):
You can’t perform that action at this time.
0 commit comments