From 73d7035ad80415b19c36724a91cc01966b91552f Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Tue, 9 Jan 2018 01:01:49 -0600 Subject: [PATCH] No SettingWithCopyWarning on groupby results The dataframe referred to in the warning is a result of an internal slicing operation and should not lead to a warning in userspace. fixes #19151 --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/core/groupby.py | 4 +++- pandas/tests/groupby/test_groupby.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index d7a3f0d077302..79479d081e72f 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -425,6 +425,7 @@ Groupby/Resample/Rolling - Bug when grouping by a single column and aggregating with a class like ``list`` or ``tuple`` (:issue:`18079`) - Fixed regression in :func:`DataFrame.groupby` which would not emit an error when called with a tuple key not in the index (:issue:`18798`) +- Modifying a group dataframe while generating ``groupby`` results does not trigger a ``SettingWithCopyWarning`` (:issue:`19151`) - - diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 082b6e2a8b1a0..e08df6877a177 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -4722,7 +4722,9 @@ def __iter__(self): # if start >= end: # raise AssertionError('Start %s must be less than end %s' # % (str(start), str(end))) - yield i, self._chop(sdata, slice(start, end)) + group_data = self._chop(sdata, slice(start, end)) + group_data._is_copy = None + yield i, group_data def _get_sorted_data(self): return self.data._take(self.sort_idx, axis=self.axis, convert=False) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 5172efe25d697..0e756d331a301 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2758,6 +2758,12 @@ def test_tuple_correct_keyerror(self): with tm.assert_raises_regex(KeyError, "(7, 8)"): df.groupby((7, 8)).mean() + def test_no_setting_with_copy_warning(self): + df = pd.DataFrame({'x': range(4), 'c': list('aabb')}) + for _, gdf in df.groupby('c'): + with tm.assert_produces_warning(None): + gdf['x'] = 1 + def _check_groupby(df, result, keys, field, f=lambda x: x.sum()): tups = lmap(tuple, df[keys].values)