Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMPAT: remove SettingWithCopy warning, and use copy-on-write, #10954 #10973

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
class PandasError(Exception):
pass


class SettingWithCopyError(ValueError):
class SettingImmutableError(ValueError):
pass


class SettingWithCopyWarning(Warning):
class SettingWithCopyError(ValueError):
pass


class AmbiguousIndexError(PandasError, KeyError):
pass

Expand Down
9 changes: 4 additions & 5 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,12 @@ def use_inf_as_null_cb(key):
# user warnings
chained_assignment = """
: string
Raise an exception, warn, or no action if trying to use chained assignment,
The default is warn
this option has been deprecated and has no effect
"""

with cf.config_prefix('mode'):
cf.register_option('chained_assignment', 'warn', chained_assignment,
validator=is_one_of_factory([None, 'warn', 'raise']))
cf.register_option('mode.chained_assignment', 'warn', chained_assignment,
validator=is_one_of_factory([None, 'warn', 'raise']))
cf.deprecate_option('mode.chained_assignment', chained_assignment)


# Set up the io.excel specific configuration.
Expand Down
34 changes: 20 additions & 14 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ def _ixs(self, i, axis=0):
copy = isinstance(new_values,np.ndarray) and new_values.base is None
result = Series(new_values, index=self.columns,
name=self.index[i], dtype=new_values.dtype)
result._set_is_copy(self, copy=copy)
result._set_parent(self, copy=copy)
return result

# icol
Expand Down Expand Up @@ -1957,7 +1957,7 @@ def _getitem_multilevel(self, key):
if isinstance(result, Series):
result = self._constructor_sliced(result, index=self.index, name=key)

result._set_is_copy(self)
result._set_parent(self)
return result
else:
return self._get_item_cache(key)
Expand Down Expand Up @@ -2229,7 +2229,7 @@ def __setitem__(self, key, value):
self._set_item(key, value)

def _setitem_slice(self, key, value):
self._check_setitem_copy()
self._check_copy_on_write()
self.ix._setitem_with_indexer(key, value)

def _setitem_array(self, key, value):
Expand All @@ -2240,7 +2240,7 @@ def _setitem_array(self, key, value):
(len(key), len(self.index)))
key = check_bool_indexer(self.index, key)
indexer = key.nonzero()[0]
self._check_setitem_copy()
self._check_copy_on_write()
self.ix._setitem_with_indexer(indexer, value)
else:
if isinstance(value, DataFrame):
Expand All @@ -2250,7 +2250,7 @@ def _setitem_array(self, key, value):
self[k1] = value[k2]
else:
indexer = self.ix._convert_to_indexer(key, axis=1)
self._check_setitem_copy()
self._check_copy_on_write()
self.ix._setitem_with_indexer((slice(None), indexer), value)

def _setitem_frame(self, key, value):
Expand All @@ -2260,7 +2260,7 @@ def _setitem_frame(self, key, value):
raise TypeError('Must pass DataFrame with boolean values only')

self._check_inplace_setting(value)
self._check_setitem_copy()
self._check_copy_on_write()
self.where(-key, value, inplace=True)

def _ensure_valid_index(self, value):
Expand Down Expand Up @@ -2293,14 +2293,20 @@ def _set_item(self, key, value):
"""

self._ensure_valid_index(value)
value = self._sanitize_column(key, value)
NDFrame._set_item(self, key, value)

# check if we are modifying a copy
# try to set first as we want an invalid
# value exeption to occur first
if len(self):
self._check_setitem_copy()
svalue = self._sanitize_column(key, value)
try:
NDFrame._set_item(self, key, svalue)
except com.SettingWithCopyError:

# if we have a multi-index (which potentially has dropped levels)
# need to raise
for p in self._parent:
if isinstance(getattr(p(),'columns',None), MultiIndex):
raise

# we have a chained assignment
# assign back to the original
self._parent[0]().loc[self.index,key] = value

def insert(self, loc, column, value, allow_duplicates=False):
"""
Expand Down
Loading