1919from pandas import compat , _np_version_under1p7
2020from pandas .compat import map , zip , lrange , string_types , isidentifier
2121from pandas .core .common import (isnull , notnull , is_list_like ,
22- _values_from_object , _maybe_promote , ABCSeries )
22+ _values_from_object , _maybe_promote , ABCSeries ,
23+ SettingWithCopyError , SettingWithCopyWarning )
2324import pandas .core .nanops as nanops
2425from pandas .util .decorators import Appender , Substitution
26+ from pandas .core import config
2527
2628# goal is to be able to define the docs close to function, while still being
2729# able to share
@@ -69,7 +71,7 @@ class NDFrame(PandasObject):
6971 copy : boolean, default False
7072 """
7173 _internal_names = [
72- '_data' , 'name' , '_cacher' , '_subtyp' , '_index' , '_default_kind' , '_default_fill_value' ]
74+ '_data' , 'name' , '_cacher' , '_is_copy' , ' _subtyp' , '_index' , '_default_kind' , '_default_fill_value' ]
7375 _internal_names_set = set (_internal_names )
7476 _metadata = []
7577
@@ -85,6 +87,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None, fastpath=False):
8587 for i , ax in enumerate (axes ):
8688 data = data .reindex_axis (ax , axis = i )
8789
90+ object .__setattr__ (self , '_is_copy' , False )
8891 object .__setattr__ (self , '_data' , data )
8992 object .__setattr__ (self , '_item_cache' , {})
9093
@@ -988,6 +991,22 @@ def _set_item(self, key, value):
988991 self ._data .set (key , value )
989992 self ._clear_item_cache ()
990993
994+ def _setitem_copy (self , copy ):
995+ """ set the _is_copy of the iiem """
996+ self ._is_copy = copy
997+ return self
998+
999+ def _check_setitem_copy (self ):
1000+ """ validate if we are doing a settitem on a chained copy """
1001+ if self ._is_copy :
1002+ value = config ._get_option_fast ('mode.chained_assignment' )
1003+
1004+ t = "A value is trying to be set on a copy of a slice from a DataFrame.\n Try using .loc[row_index,col_indexer] = value instead"
1005+ if value == 'raise' :
1006+ raise SettingWithCopyError (t )
1007+ elif value == 'warn' :
1008+ warnings .warn (t ,SettingWithCopyWarning )
1009+
9911010 def __delitem__ (self , key ):
9921011 """
9931012 Delete item
@@ -1049,7 +1068,7 @@ def take(self, indices, axis=0, convert=True):
10491068 new_data = self ._data .reindex_axis (new_items , indexer = indices , axis = 0 )
10501069 else :
10511070 new_data = self ._data .take (indices , axis = baxis )
1052- return self ._constructor (new_data ).__finalize__ (self )
1071+ return self ._constructor (new_data )._setitem_copy ( True ). __finalize__ (self )
10531072
10541073 # TODO: Check if this was clearer in 0.12
10551074 def select (self , crit , axis = 0 ):
0 commit comments