Skip to content

Commit 1d65e9f

Browse files
committed
BUG: Fix pandas-dev#10355, std() groupby calculation
1 parent 64104ec commit 1d65e9f

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
@@ -1190,8 +1190,9 @@ def std(self, ddof=1, *args, **kwargs):
11901190
"""
11911191

11921192
# TODO: implement at Cython level?
1193-
nv.validate_groupby_func('std', args, kwargs)
1194-
return np.sqrt(self.var(ddof=ddof, **kwargs))
1193+
with _group_selection_context(self):
1194+
f = lambda x: x.std(axis=self.axis, **kwargs)
1195+
return self._python_agg_general(f)
11951196

11961197
@Substitution(name='groupby')
11971198
@Appender(_common_see_also)

pandas/tests/groupby/test_groupby.py

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

2929

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

0 commit comments

Comments
 (0)