Skip to content

No SettingWithCopyWarning on groupby results #19152

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

Closed
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-
-

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down