Skip to content

API: spurious setting_with_copy warning (GH8730) #9659

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

Merged
merged 1 commit into from
Mar 15, 2015
Merged
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
9 changes: 9 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ Other API Changes

To reproduce the old behavior, simply add more precision to the label (e.g., use ``2000-02-01`` instead of ``2000-02``).

- A Spurious ``SettingWithCopy`` Warning was generated when setting a new item in a frame in some cases (:issue:`8730`)

The following would previously report a ``SettingWithCopy`` Warning.

.. ipython:: python

df1 = DataFrame({'x': Series(['a','b','c']), 'y': Series(['d','e','f'])})
df2 = df1[['x']]
df2['y'] = ['g', 'h', 'i']

.. _whatsnew_0160.deprecations:

Expand Down
13 changes: 11 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,14 @@ def _check_setitem_copy(self, stacklevel=4, t='setting', force=False):
except:
pass

# we might be a false positive
try:
if self.is_copy().shape == self.shape:
self.is_copy = None
return
except:
pass

# a custom message
if isinstance(self.is_copy, string_types):
t = self.is_copy
Expand Down Expand Up @@ -1344,8 +1352,9 @@ def take(self, indices, axis=0, convert=True, is_copy=True):
result = self._constructor(new_data).__finalize__(self)

# maybe set copy if we didn't actually change the index
if is_copy and not result._get_axis(axis).equals(self._get_axis(axis)):
result._set_is_copy(self)
if is_copy:
if not result._get_axis(axis).equals(self._get_axis(axis)):
result._set_is_copy(self)

return result

Expand Down
26 changes: 18 additions & 8 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3751,14 +3751,6 @@ def f():
assert_series_equal(s,df.iloc[:,0].order())
assert_series_equal(s,df[0].order())

# operating on a copy
df = pd.DataFrame({'a': list(range(4)), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']})
mask = pd.isnull(df.c)

def f():
df[['c']][mask] = df[['b']][mask]
self.assertRaises(com.SettingWithCopyError, f)

# false positives GH6025
df = DataFrame ({'column1':['a', 'a', 'a'], 'column2': [4,8,9] })
str(df)
Expand Down Expand Up @@ -3790,6 +3782,24 @@ def f():
df['C'][2] = 'foo'
self.assertRaises(com.SettingWithCopyError, f)

def test_setting_with_copy_bug(self):

# operating on a copy
df = pd.DataFrame({'a': list(range(4)), 'b': list('ab..'), 'c': ['a', 'b', np.nan, 'd']})
mask = pd.isnull(df.c)

def f():
df[['c']][mask] = df[['b']][mask]
self.assertRaises(com.SettingWithCopyError, f)

# invalid warning as we are returning a new object
# GH 8730
df1 = DataFrame({'x': Series(['a','b','c']), 'y': Series(['d','e','f'])})
df2 = df1[['x']]

# this should not raise
df2['y'] = ['g', 'h', 'i']

def test_detect_chained_assignment_warnings(self):

# warnings
Expand Down