Skip to content

... #25315

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 3 commits into from
Closed

... #25315

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
14 changes: 11 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,10 +1171,18 @@ 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:
Copy link
Member

Choose a reason for hiding this comment

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

Not entirely clear what this is for but except for legacy code we never catch just base Exception so will need something more explicit

Copy link
Member

Choose a reason for hiding this comment

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

Copy / paste isn't the right way to go here. Can you still keep the dispatch to self.var and just see what is mangling the expression once passed as an argument to np.sqrt instead?

f = lambda x: x.std(ddof=ddof, **kwargs)
with _group_selection_context(self):
return self._python_agg_general(f)
else:
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 Down
32 changes: 32 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,3 +1698,35 @@ def test_groupby_agg_ohlc_non_first():
result = df.groupby(pd.Grouper(freq='D')).agg(['sum', 'ohlc'])

tm.assert_frame_equal(result, expected)


def test_groupby_std_with_as_index_false():
# https://github.com/pandas-dev/pandas/issues/16799

df = pd.DataFrame({
"A": ["a", "b", "a", "b"],
"B": ["A", "B", "A", "B"],
"X": [1, 2, 3, 4]
})

group_var = df.groupby(
["A", "B"],
as_index=False,
).var()

# A B X
# 0 a A 2
# 1 b B 2

group_std = df.groupby(
["A", "B"],
as_index=False,
).std()

# A B X
# 0 a A 1.414214
# 1 b B 1.414214

assert_series_equal(
np.sqrt(group_var["X"]),
Copy link
Member

Choose a reason for hiding this comment

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

Unless there is a reason for using np.sqrt here just explicitly construct the expected values in a variable called expected. Also do result = on the groupby op you are testing and simply make this line assert_series_equal(result, expected) (should see this usage throughout tests if unclear from this comment)

group_std["X"])