Skip to content

BUG: Fix #10355, std() groupby calculation #26229

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 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 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.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.groupby.GroupBy.idxmax` and :meth:`pandas.core.groupby.GroupBy.idxmin` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
- Bug in :meth:`pandas.core.groupby.GroupBy.cumsum`, :meth:`pandas.core.groupby.GroupBy.cumprod`, :meth:`pandas.core.groupby.GroupBy.cummin` and :meth:`pandas.core.groupby.GroupBy.cummax` with categorical column having absent categories, would return incorrect result or segfault (:issue:`16771`)
- Bug in :meth:`pandas.core.groupby.GroupBy.nth` where NA values in the grouping would return incorrect results (:issue:`26011`)
- Bug in :meth:`pandas.core.groupby.GroupBy.std` that computed standard deviation without respecting groupby context when `as_index=False` (:issue:`10355`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use double back ticks around as_index=False



Reshaping
Expand Down
24 changes: 14 additions & 10 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,10 +1185,16 @@ def std(self, ddof=1, *args, **kwargs):
ddof : integer, default 1
degrees of freedom
"""

# TODO: implement at Cython level?
nv.validate_groupby_func('std', args, kwargs)
return np.sqrt(self.var(ddof=ddof, **kwargs))
if ddof == 1:
try:
return self._cython_agg_general('std', **kwargs)
except Exception:
pass

f = lambda x: x.std(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)

@Substitution(name='groupby')
@Appender(_common_see_also)
Expand All @@ -1208,13 +1214,11 @@ def var(self, ddof=1, *args, **kwargs):
try:
return self._cython_agg_general('var', **kwargs)
except Exception:
f = lambda x: x.var(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)
else:
f = lambda x: x.var(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)
pass

f = lambda x: x.var(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)

@Substitution(name='groupby')
@Appender(_common_see_also)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def get_group_levels(self):
'name': 'group_median'
},
'var': 'group_var',
'std': {
'name': 'group_var_bin',
'f': lambda func, a: np.sqrt(func(a)),
},
'first': {
'name': 'group_nth',
'f': lambda func, a, b, c, d, e: func(a, b, c, d, 1, -1)
Expand Down
32 changes: 21 additions & 11 deletions pandas/tests/groupby/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,43 @@ def raw_frame():
@pytest.mark.parametrize('axis', [0, 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this and it will just use the axis fixture; note that axis will take on [0, 1, 'index', 'columns'], so you may have to adjust some of the test conditions

@pytest.mark.parametrize('skipna', [True, False])
@pytest.mark.parametrize('sort', [True, False])
@pytest.mark.parametrize('as_index', [True, False])
def test_regression_whitelist_methods(
raw_frame, op, level,
axis, skipna, sort):
axis, skipna, sort, as_index):
# GH6944
# GH 17537
# explicitly test the whitelist methods

if not as_index and axis == 1:
pytest.skip('as_index=False only valid for axis=0')

if axis == 0:
frame = raw_frame
else:
frame = raw_frame.T

grouped = frame.groupby(level=level, axis=axis, sort=sort,
as_index=as_index)

if op in AGG_FUNCTIONS_WITH_SKIPNA:
grouped = frame.groupby(level=level, axis=axis, sort=sort)
result = getattr(grouped, op)(skipna=skipna)
expected = getattr(frame, op)(level=level, axis=axis,
skipna=skipna)
if sort:
expected = expected.sort_index(axis=axis, level=level)
tm.assert_frame_equal(result, expected)
expected = getattr(frame, op)(level=level, axis=axis, skipna=skipna)
else:
grouped = frame.groupby(level=level, axis=axis, sort=sort)
result = getattr(grouped, op)()
expected = getattr(frame, op)(level=level, axis=axis)
if sort:
expected = expected.sort_index(axis=axis, level=level)
tm.assert_frame_equal(result, expected)

if sort:
expected = expected.sort_index(axis=axis, level=level)

if not as_index:
expected = expected.reset_index()
if level == 0:
expected = expected.drop(columns=['first'])
if level == 1:
expected = expected.drop(columns=['second'])

tm.assert_frame_equal(result, expected)


def test_groupby_blacklist(df_letters):
Expand Down