@@ -3263,58 +3263,50 @@ def _check_setitem_copy(self, stacklevel=4, t='setting', force=False):
3263
3263
3264
3264
"""
3265
3265
3266
- if force or self ._is_copy :
3266
+ # return early if the check is not needed
3267
+ if not (force or self ._is_copy ):
3268
+ return
3267
3269
3268
- value = config .get_option ('mode.chained_assignment' )
3269
- if value is None :
3270
- return
3271
-
3272
- # see if the copy is not actually referred; if so, then dissolve
3273
- # the copy weakref
3274
- try :
3275
- gc .collect (2 )
3276
- if not gc .get_referents (self ._is_copy ()):
3277
- self ._is_copy = None
3278
- return
3279
- except Exception :
3280
- pass
3270
+ value = config .get_option ('mode.chained_assignment' )
3271
+ if value is None :
3272
+ return
3281
3273
3282
- # we might be a false positive
3283
- try :
3284
- if self ._is_copy (). shape == self . shape :
3285
- self ._is_copy = None
3286
- return
3287
- except Exception :
3288
- pass
3274
+ # see if the copy is not actually referred; if so, then dissolve
3275
+ # the copy weakref
3276
+ if self . _is_copy is not None and not isinstance ( self ._is_copy , str ) :
3277
+ r = self ._is_copy ()
3278
+ if not gc . get_referents ( r ) or r . shape == self . shape :
3279
+ self . _is_copy = None
3280
+ return
3289
3281
3290
- # a custom message
3291
- if isinstance (self ._is_copy , str ):
3292
- t = self ._is_copy
3282
+ # a custom message
3283
+ if isinstance (self ._is_copy , str ):
3284
+ t = self ._is_copy
3293
3285
3294
- elif t == 'referant' :
3295
- t = ("\n "
3296
- "A value is trying to be set on a copy of a slice from a "
3297
- "DataFrame\n \n "
3298
- "See the caveats in the documentation: "
3299
- "http://pandas.pydata.org/pandas-docs/stable/user_guide/"
3300
- "indexing.html#returning-a-view-versus-a-copy"
3301
- )
3286
+ elif t == 'referant' :
3287
+ t = ("\n "
3288
+ "A value is trying to be set on a copy of a slice from a "
3289
+ "DataFrame\n \n "
3290
+ "See the caveats in the documentation: "
3291
+ "http://pandas.pydata.org/pandas-docs/stable/user_guide/"
3292
+ "indexing.html#returning-a-view-versus-a-copy"
3293
+ )
3302
3294
3303
- else :
3304
- t = ("\n "
3305
- "A value is trying to be set on a copy of a slice from a "
3306
- "DataFrame.\n "
3307
- "Try using .loc[row_indexer,col_indexer] = value "
3308
- "instead\n \n See the caveats in the documentation: "
3309
- "http://pandas.pydata.org/pandas-docs/stable/user_guide/"
3310
- "indexing.html#returning-a-view-versus-a-copy"
3311
- )
3312
-
3313
- if value == 'raise' :
3314
- raise com .SettingWithCopyError (t )
3315
- elif value == 'warn' :
3316
- warnings .warn (t , com .SettingWithCopyWarning ,
3317
- stacklevel = stacklevel )
3295
+ else :
3296
+ t = ("\n "
3297
+ "A value is trying to be set on a copy of a slice from a "
3298
+ "DataFrame.\n "
3299
+ "Try using .loc[row_indexer,col_indexer] = value "
3300
+ "instead\n \n See the caveats in the documentation: "
3301
+ "http://pandas.pydata.org/pandas-docs/stable/user_guide/"
3302
+ "indexing.html#returning-a-view-versus-a-copy"
3303
+ )
3304
+
3305
+ if value == 'raise' :
3306
+ raise com .SettingWithCopyError (t )
3307
+ elif value == 'warn' :
3308
+ warnings .warn (t , com .SettingWithCopyWarning ,
3309
+ stacklevel = stacklevel )
3318
3310
3319
3311
def __delitem__ (self , key ):
3320
3312
"""
0 commit comments