@@ -59,7 +59,7 @@ def _single_replace(self, to_replace, method, inplace, limit):
5959 dtype = self .dtype ).__finalize__ (self )
6060
6161 if inplace :
62- self ._data = result ._data
62+ self ._update_inplace ( result ._data )
6363 return
6464
6565 return result
@@ -562,9 +562,7 @@ def f(x):
562562 result ._clear_item_cache ()
563563
564564 if inplace :
565- self ._data = result ._data
566- self ._clear_item_cache ()
567-
565+ self ._update_inplace (result ._data )
568566 else :
569567 return result .__finalize__ (self )
570568
@@ -994,12 +992,22 @@ def _maybe_update_cacher(self, clear=False):
994992 if clear, then clear our cache """
995993 cacher = getattr (self , '_cacher' , None )
996994 if cacher is not None :
997- try :
998- cacher [1 ]()._maybe_cache_changed (cacher [0 ], self )
999- except :
995+ ref = cacher [1 ]()
1000996
1001- # our referant is dead
997+ # we are trying to reference a dead referant, hence
998+ # a copy
999+ if ref is None :
10021000 del self ._cacher
1001+ self .is_copy = True
1002+ self ._check_setitem_copy (stacklevel = 5 , t = 'referant' )
1003+ else :
1004+ try :
1005+ ref ._maybe_cache_changed (cacher [0 ], self )
1006+ except :
1007+ pass
1008+ if ref .is_copy :
1009+ self .is_copy = True
1010+ self ._check_setitem_copy (stacklevel = 5 , t = 'referant' )
10031011
10041012 if clear :
10051013 self ._clear_item_cache ()
@@ -1019,17 +1027,21 @@ def _setitem_copy(self, copy):
10191027 self .is_copy = copy
10201028 return self
10211029
1022- def _check_setitem_copy (self , stacklevel = 4 ):
1030+ def _check_setitem_copy (self , stacklevel = 4 , t = 'setting' ):
10231031 """ validate if we are doing a settitem on a chained copy.
10241032
10251033 If you call this function, be sure to set the stacklevel such that the
10261034 user will see the error *at the level of setting*"""
10271035 if self .is_copy :
10281036 value = config .get_option ('mode.chained_assignment' )
10291037
1030- t = ("A value is trying to be set on a copy of a slice from a "
1031- "DataFrame.\n Try using .loc[row_index,col_indexer] = value "
1032- "instead" )
1038+ if t == 'referant' :
1039+ t = ("A value is trying to be set on a copy of a slice from a "
1040+ "DataFrame" )
1041+ else :
1042+ t = ("A value is trying to be set on a copy of a slice from a "
1043+ "DataFrame.\n Try using .loc[row_index,col_indexer] = value "
1044+ "instead" )
10331045 if value == 'raise' :
10341046 raise SettingWithCopyError (t )
10351047 elif value == 'warn' :
@@ -1218,7 +1230,7 @@ def _update_inplace(self, result):
12181230 # decision that we may revisit in the future.
12191231 self ._reset_cache ()
12201232 self ._clear_item_cache ()
1221- self ._data = result . _data
1233+ self ._data = getattr ( result , ' _data' , result )
12221234 self ._maybe_update_cacher ()
12231235
12241236 def add_prefix (self , prefix ):
@@ -1910,14 +1922,13 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
19101922 continue
19111923 obj = result [k ]
19121924 obj .fillna (v , inplace = True )
1913- obj ._maybe_update_cacher ()
19141925 return result
19151926 else :
19161927 new_data = self ._data .fillna (value , inplace = inplace ,
19171928 downcast = downcast )
19181929
19191930 if inplace :
1920- self ._data = new_data
1931+ self ._update_inplace ( new_data )
19211932 else :
19221933 return self ._constructor (new_data ).__finalize__ (self )
19231934
@@ -2165,7 +2176,7 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
21652176 new_data = new_data .convert (copy = not inplace , convert_numeric = False )
21662177
21672178 if inplace :
2168- self ._data = new_data
2179+ self ._update_inplace ( new_data )
21692180 else :
21702181 return self ._constructor (new_data ).__finalize__ (self )
21712182
@@ -2272,10 +2283,10 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
22722283
22732284 if inplace :
22742285 if axis == 1 :
2275- self ._data = new_data
2286+ self ._update_inplace ( new_data )
22762287 self = self .T
22772288 else :
2278- self ._data = new_data
2289+ self ._update_inplace ( new_data )
22792290 else :
22802291 res = self ._constructor (new_data ).__finalize__ (self )
22812292 if axis == 1 :
@@ -2856,8 +2867,9 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
28562867 if inplace :
28572868 # we may have different type blocks come out of putmask, so
28582869 # reconstruct the block manager
2859- self ._data = self ._data .putmask (cond , other , align = axis is None ,
2860- inplace = True )
2870+ new_data = self ._data .putmask (cond , other , align = axis is None ,
2871+ inplace = True )
2872+ self ._update_inplace (new_data )
28612873
28622874 else :
28632875 new_data = self ._data .where (other , cond , align = axis is None ,
0 commit comments