Skip to content

Commit 14eb325

Browse files
committed
BUG: Fix pandas-dev#10355, std() groupby calculation
1 parent ca1a36a commit 14eb325

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pandas/core/groupby/groupby.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,9 @@ def std(self, ddof=1, *args, **kwargs):
11871187
"""
11881188

11891189
# TODO: implement at Cython level?
1190-
nv.validate_groupby_func('std', args, kwargs)
1191-
return np.sqrt(self.var(ddof=ddof, **kwargs))
1190+
with _group_selection_context(self):
1191+
f = lambda x: x.std(axis=self.axis, **kwargs)
1192+
return self._python_agg_general(f)
11921193

11931194
@Substitution(name='groupby')
11941195
@Appender(_common_see_also)

pandas/tests/groupby/test_groupby.py

+25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ def test_repr():
2626
assert result == expected
2727

2828

29+
def test_groupby_std():
30+
# GH10355
31+
df = pd.DataFrame({
32+
'a': [1, 1, 1, 2, 2, 2, 3, 3, 3],
33+
'b': [1, 2, 3, 3, 5, 7, 7, 8, 9],
34+
})
35+
result = df.groupby('a', as_index=False).std()
36+
expected = pd.DataFrame({
37+
'a': [1, 2, 3],
38+
'b': [1, 2, 1]
39+
})
40+
assert_frame_equal(result, expected)
41+
42+
df = pd.DataFrame({
43+
'a': [1, 1, 1, 2, 2, 2, 3, 3, 3],
44+
'b': [1, 2, 3, 3, 5, 7, 7, 8, 9],
45+
})
46+
result = df.groupby('a', as_index=True).std()
47+
expected = pd.DataFrame({
48+
'a': [1, 2, 3],
49+
'b': [1, 2, 1]
50+
}).set_index('a')
51+
assert_frame_equal(result, expected)
52+
53+
2954
@pytest.mark.parametrize('dtype', ['int64', 'int32', 'float64', 'float32'])
3055
def test_basic(dtype):
3156

0 commit comments

Comments
 (0)