From 616e6cb213e1e4202753983fed32f51a282a1d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AC=9D=E5=AE=87=E6=81=86?= Date: Thu, 14 Feb 2019 12:51:10 +0800 Subject: [PATCH 1/3] FIX: apply std to groupby with as_index=False - https://github.com/pandas-dev/pandas/issues/16799 - https://github.com/pandas-dev/pandas/issues/10355 --- pandas/core/groupby/groupby.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c63bc5164e25b..e2bda6c683bf3 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1172,9 +1172,18 @@ def std(self, ddof=1, *args, **kwargs): 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: + 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) From dd6137b89d0b38371c20e06859d1b4a9cf6a0c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AC=9D=E5=AE=87=E6=81=86?= Date: Thu, 14 Feb 2019 12:55:01 +0800 Subject: [PATCH 2/3] remove extra newline --- pandas/core/groupby/groupby.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index e2bda6c683bf3..921be7a63176d 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1171,7 +1171,6 @@ def std(self, ddof=1, *args, **kwargs): ddof : integer, default 1 degrees of freedom """ - nv.validate_groupby_func('std', args, kwargs) if ddof == 1: try: From 89d39f17fdebfcfe7177b6f82ea9d9666d19ecc5 Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Thu, 14 Feb 2019 18:56:51 +0800 Subject: [PATCH 3/3] test_groupby_std_with_as_index_false --- pandas/tests/groupby/test_groupby.py | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 1ae8efd2f6867..630ac883b40ca 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -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"]), + group_std["X"])