@@ -581,8 +581,11 @@ def test_loc_setitem_consistency(self, frame_for_consistency, val):
581581 # GH 6149
582582 # coerce similarly for setitem and loc when rows have a null-slice
583583 df = frame_for_consistency .copy ()
584+ msg = r"Setting `df.loc\[:, col\] = values` does \*not\* change"
585+ err = UserWarning if isinstance (val , np .ndarray ) else None
584586 with pytest .raises (TypeError , match = "Invalid value" ):
585- df .loc [:, "date" ] = val
587+ with tm .assert_produces_warning (err , match = msg ):
588+ df .loc [:, "date" ] = val
586589
587590 def test_loc_setitem_consistency_dt64_to_str (self , frame_for_consistency ):
588591 # GH 6149
@@ -646,18 +649,21 @@ def test_loc_setitem_consistency_slice_column_len(self, using_infer_string):
646649 ]
647650 df = DataFrame (values , index = mi , columns = cols )
648651
652+ msg = r"Setting `df.loc\[:, col\] = values` does \*not\* change"
649653 ctx = contextlib .nullcontext ()
650654 if using_infer_string :
651655 ctx = pytest .raises (TypeError , match = "Invalid value" )
652656
653657 with ctx :
654- df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
655- df .loc [:, ("Respondent" , "StartDate" )]
656- )
658+ with tm .assert_produces_warning (UserWarning , match = msg ):
659+ df .loc [:, ("Respondent" , "StartDate" )] = to_datetime (
660+ df .loc [:, ("Respondent" , "StartDate" )]
661+ )
657662 with ctx :
658- df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
659- df .loc [:, ("Respondent" , "EndDate" )]
660- )
663+ with tm .assert_produces_warning (UserWarning , match = msg ):
664+ df .loc [:, ("Respondent" , "EndDate" )] = to_datetime (
665+ df .loc [:, ("Respondent" , "EndDate" )]
666+ )
661667
662668 if using_infer_string :
663669 # infer-objects won't infer stuff anymore
@@ -1426,7 +1432,9 @@ def test_loc_setitem_single_row_categorical(self, using_infer_string):
14261432
14271433 # pre-2.0 this swapped in a new array, in 2.0 it operates inplace,
14281434 # consistent with non-split-path
1429- df .loc [:, "Alpha" ] = categories
1435+ msg = r"Setting `df.loc\[:, col\] = values` does \*not\* change"
1436+ with tm .assert_produces_warning (UserWarning , match = msg ):
1437+ df .loc [:, "Alpha" ] = categories
14301438
14311439 result = df ["Alpha" ]
14321440 expected = Series (categories , index = df .index , name = "Alpha" ).astype (
0 commit comments