Skip to content

BUG: Fixed bug in groupby.std changing target column when as_index=False #11085

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.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ Bug Fixes
- Bug in ``algos.outer_join_indexer`` when ``right`` array is empty (:issue:`10618`)

- Bug in ``filter`` (regression from 0.16.0) and ``transform`` when grouping on multiple keys, one of which is datetime-like (:issue:`10114`)
- Bug in ``groupby(as_index=False)`` with ``std`` accidentally modifying target column at the same time (:issue:`10355`)


- Bug in ``to_datetime`` and ``to_timedelta`` causing ``Index`` name to be lost (:issue:`10875`)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,9 @@ def std(self, ddof=1):

For multiple groupings, the result index will be a MultiIndex
"""
# todo, implement at cython level?
return np.sqrt(self.var(ddof=ddof))
self._set_selection_from_grouper()
f = lambda x: np.sqrt(x.var(ddof=ddof))
return self._python_agg_general(f)

def var(self, ddof=1):
"""
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,20 @@ def _check_results(grouped):
lambda x: x.weekday()])
_check_results(by_mwkday)

# issue 10355
def test_std(self):
df = pd.DataFrame({
'a' : [1,1,1,2,2,2,3,3,3],
'b' : [1,2,3,4,5,6,7,8,9],
})
result = df.groupby('a',as_index=False).std()
expected = pd.DataFrame({
'a' : [1, 2, 3],
'b' : [1, 1, 1]
})
assert_frame_equal(result, expected)


def test_aggregate_item_by_item(self):

df = self.df.copy()
Expand Down