19
19
from pandas import compat , _np_version_under1p7
20
20
from pandas .compat import map , zip , lrange , string_types , isidentifier
21
21
from 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 )
23
24
import pandas .core .nanops as nanops
24
25
from pandas .util .decorators import Appender , Substitution
26
+ from pandas .core import config
25
27
26
28
# goal is to be able to define the docs close to function, while still being
27
29
# able to share
@@ -69,7 +71,7 @@ class NDFrame(PandasObject):
69
71
copy : boolean, default False
70
72
"""
71
73
_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' ]
73
75
_internal_names_set = set (_internal_names )
74
76
_metadata = []
75
77
@@ -85,6 +87,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None, fastpath=False):
85
87
for i , ax in enumerate (axes ):
86
88
data = data .reindex_axis (ax , axis = i )
87
89
90
+ object .__setattr__ (self , '_is_copy' , False )
88
91
object .__setattr__ (self , '_data' , data )
89
92
object .__setattr__ (self , '_item_cache' , {})
90
93
@@ -988,6 +991,22 @@ def _set_item(self, key, value):
988
991
self ._data .set (key , value )
989
992
self ._clear_item_cache ()
990
993
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
+
991
1010
def __delitem__ (self , key ):
992
1011
"""
993
1012
Delete item
@@ -1049,7 +1068,7 @@ def take(self, indices, axis=0, convert=True):
1049
1068
new_data = self ._data .reindex_axis (new_items , indexer = indices , axis = 0 )
1050
1069
else :
1051
1070
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 )
1053
1072
1054
1073
# TODO: Check if this was clearer in 0.12
1055
1074
def select (self , crit , axis = 0 ):
0 commit comments