From 5951cf3daa83af985d2fba8238a0a29d47e8ece0 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Thu, 27 Feb 2020 20:18:50 +0800 Subject: [PATCH 1/5] ENH: moved corrwith to from transformation to reduction kernels (GH31270) --- pandas/core/groupby/base.py | 2 +- pandas/tests/groupby/test_categorical.py | 3 +++ pandas/tests/groupby/test_transform.py | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 700d8d503d086..363286704ba95 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -98,6 +98,7 @@ def _gotitem(self, key, ndim, subset=None): [ "all", "any", + "corrwith", "count", "first", "idxmax", @@ -132,7 +133,6 @@ def _gotitem(self, key, ndim, subset=None): [ "backfill", "bfill", - "corrwith", "cumcount", "cummax", "cummin", diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 9b07269811d8e..622d71d694a45 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1262,6 +1262,9 @@ def test_series_groupby_on_2_categoricals_unobserved( if reduction_func == "ngroup": pytest.skip("ngroup is not truly a reduction") + if reduction_func == "corrwith": # GH + pytest.xfail("TODO: implemented SeriesGroupBy.corrwith") + df = pd.DataFrame( { "cat_1": pd.Categorical(list("AABB"), categories=list("ABCD")), diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index 740103eec185a..d6a243d73ba31 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1093,6 +1093,8 @@ def test_transform_agg_by_name(reduction_func, obj): pytest.xfail("TODO: g.transform('ngroup') doesn't work") if func == "size": # GH#27469 pytest.xfail("TODO: g.transform('size') doesn't work") + if func == "corrwith": # GH + pytest.xfail("TODO: rationalize implementation of DataFrameGroupBy.corrwith") args = {"nth": [0], "quantile": [0.5]}.get(func, []) From 62e778f190753b188c8c46e1282d17857ae0d7bb Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Thu, 27 Feb 2020 21:02:31 +0800 Subject: [PATCH 2/5] ENH: moved corrwith from transformation to reduction kernels (GH31270) --- pandas/core/groupby/base.py | 2 ++ pandas/tests/groupby/test_categorical.py | 2 +- pandas/tests/groupby/test_transform.py | 10 +++++----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 363286704ba95..bae8a8e519f20 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -98,6 +98,8 @@ def _gotitem(self, key, ndim, subset=None): [ "all", "any", + # `corrwith` is a reduction as long as `other` is a Series + # or has only 1 column with the same name "corrwith", "count", "first", diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 622d71d694a45..9ea5252b91e13 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1262,7 +1262,7 @@ def test_series_groupby_on_2_categoricals_unobserved( if reduction_func == "ngroup": pytest.skip("ngroup is not truly a reduction") - if reduction_func == "corrwith": # GH + if reduction_func == "corrwith": # GH 32293 pytest.xfail("TODO: implemented SeriesGroupBy.corrwith") df = pd.DataFrame( diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index d6a243d73ba31..b9a1c94df4d92 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -327,9 +327,9 @@ def test_transform_transformation_func(transformation_func): } ) - if transformation_func in ["pad", "backfill", "tshift", "corrwith", "cumcount"]: + if transformation_func in ["pad", "backfill", "tshift", "cumcount"]: # These transformation functions are not yet covered in this test - pytest.xfail("See GH 31269 and GH 31270") + pytest.xfail("See GH 31269") elif _is_numpy_dev and transformation_func in ["cummin"]: pytest.xfail("https://github.com/pandas-dev/pandas/issues/31992") elif transformation_func == "fillna": @@ -1093,10 +1093,10 @@ def test_transform_agg_by_name(reduction_func, obj): pytest.xfail("TODO: g.transform('ngroup') doesn't work") if func == "size": # GH#27469 pytest.xfail("TODO: g.transform('size') doesn't work") - if func == "corrwith": # GH - pytest.xfail("TODO: rationalize implementation of DataFrameGroupBy.corrwith") + if func == "corrwith" and isinstance(obj, Series): # GH#32293 + pytest.xfail("TODO: implement SeriesGroupBy.corrwith") - args = {"nth": [0], "quantile": [0.5]}.get(func, []) + args = {"nth": [0], "quantile": [0.5], 'corrwith': [obj]}.get(func, []) result = g.transform(func, *args) From 1936e7a069ce1b86837219dfa496e97aa6e29a79 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Thu, 27 Feb 2020 21:06:20 +0800 Subject: [PATCH 3/5] blackified (GH31270) --- pandas/tests/groupby/test_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index b9a1c94df4d92..2295eb2297fa6 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1096,7 +1096,7 @@ def test_transform_agg_by_name(reduction_func, obj): if func == "corrwith" and isinstance(obj, Series): # GH#32293 pytest.xfail("TODO: implement SeriesGroupBy.corrwith") - args = {"nth": [0], "quantile": [0.5], 'corrwith': [obj]}.get(func, []) + args = {"nth": [0], "quantile": [0.5], "corrwith": [obj]}.get(func, []) result = g.transform(func, *args) From 113044218b2cf4da05e27f721de5d482f548fe14 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Thu, 27 Feb 2020 21:10:28 +0800 Subject: [PATCH 4/5] small change in comments (GH31270) --- pandas/core/groupby/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index bae8a8e519f20..0c9beac80761e 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -99,7 +99,6 @@ def _gotitem(self, key, ndim, subset=None): "all", "any", # `corrwith` is a reduction as long as `other` is a Series - # or has only 1 column with the same name "corrwith", "count", "first", From 408921b0798d855a5585c487974e4b0a4bab428c Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Wed, 11 Mar 2020 10:31:34 +0800 Subject: [PATCH 5/5] removed unnecessary comment (GH31270) --- pandas/core/groupby/base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 0c9beac80761e..363286704ba95 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -98,7 +98,6 @@ def _gotitem(self, key, ndim, subset=None): [ "all", "any", - # `corrwith` is a reduction as long as `other` is a Series "corrwith", "count", "first",