From 4ac557b27058fe7b4b5529019118bbb8e496ddd6 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Sun, 29 Dec 2019 13:10:11 +0800 Subject: [PATCH 1/7] TST: added test for bug in groupby pct_change (GH30463) --- pandas/tests/groupby/test_transform.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index c46180c1d11cd..e903ab8584fa1 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -874,19 +874,9 @@ def test_pad_stable_sorting(fill_method): ), ], ) -@pytest.mark.parametrize( - "periods,fill_method,limit", - [ - (1, "ffill", None), - (1, "ffill", 1), - (1, "bfill", None), - (1, "bfill", 1), - (-1, "ffill", None), - (-1, "ffill", 1), - (-1, "bfill", None), - (-1, "bfill", 1), - ], -) +@pytest.mark.parametrize("periods", [1, -1]) +@pytest.mark.parametrize("fill_method", ["ffill", "bfill", None]) +@pytest.mark.parametrize("limit", [None, 1]) def test_pct_change(test_series, freq, periods, fill_method, limit): # GH 21200, 21621 vals = [3, np.nan, np.nan, np.nan, 1, 2, 4, 10, np.nan, 4] @@ -894,7 +884,9 @@ def test_pct_change(test_series, freq, periods, fill_method, limit): key_v = np.repeat(keys, len(vals)) df = DataFrame({"key": key_v, "vals": vals * 2}) - df_g = getattr(df.groupby("key"), fill_method)(limit=limit) + df_g = df + if fill_method is not None: + df_g = getattr(df.groupby("key"), fill_method)(limit=limit) grp = df_g.groupby(df.key) expected = grp["vals"].obj / grp["vals"].shift(periods) - 1 From c8ca9c13666e37a7a2eca566cb4e9117028c9f54 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Sun, 29 Dec 2019 13:11:23 +0800 Subject: [PATCH 2/7] DOC: added whatsnew entry for bug in groupby pct_change (GH30463) --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 96ea682dd3caf..5a432f8f98250 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -869,6 +869,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`) - Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`) - Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`) +- Bug in :meth:`GroupBY.pct_change` and `SeriesGroupBy.pct_change` causes TypeError when fill_method is None (GH30463) Reshaping ^^^^^^^^^ From af67aad9b4614eb4598f086b5727fa89573ffd3b Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Sun, 29 Dec 2019 13:18:54 +0800 Subject: [PATCH 3/7] TST: added issue number in test (GH30463) --- 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 e903ab8584fa1..a9bae8ecfcf3d 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -878,7 +878,7 @@ def test_pad_stable_sorting(fill_method): @pytest.mark.parametrize("fill_method", ["ffill", "bfill", None]) @pytest.mark.parametrize("limit", [None, 1]) def test_pct_change(test_series, freq, periods, fill_method, limit): - # GH 21200, 21621 + # GH 21200, 21621, 30463 vals = [3, np.nan, np.nan, np.nan, 1, 2, 4, 10, np.nan, 4] keys = ["a", "b"] key_v = np.repeat(keys, len(vals)) From c3a4a036d0bf0bce87ea090e23a404c5b524a3cc Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Sun, 29 Dec 2019 14:03:20 +0800 Subject: [PATCH 4/7] BUG: TypeError in groupby pct_change when fill_method is None (GH30463) --- pandas/core/groupby/generic.py | 3 +++ pandas/core/groupby/groupby.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 6b110a0c80c07..b9bb2df3b48aa 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -809,6 +809,9 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None): periods=periods, fill_method=fill_method, limit=limit, freq=freq ) ) + if fill_method is None: # GH30463 + fill_method = 'pad' + limit = 0 filled = getattr(self, fill_method)(limit=limit) fill_grp = filled.groupby(self.grouper.codes) shifted = fill_grp.shift(periods=periods, freq=freq) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 227547daf3668..e64dd99f26fb2 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2362,6 +2362,9 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0 axis=axis, ) ) + if fill_method is None: # GH30463 + fill_method = 'pad' + limit = 0 filled = getattr(self, fill_method)(limit=limit) fill_grp = filled.groupby(self.grouper.codes) shifted = fill_grp.shift(periods=periods, freq=freq) From b026b8feee90cce5d9a1443022d2d03fd303ff49 Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Sun, 29 Dec 2019 14:07:11 +0800 Subject: [PATCH 5/7] CLN: reformatted code using black (GH30463) --- pandas/core/groupby/generic.py | 2 +- pandas/core/groupby/groupby.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b9bb2df3b48aa..be94fa5484496 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -810,7 +810,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None): ) ) if fill_method is None: # GH30463 - fill_method = 'pad' + fill_method = "pad" limit = 0 filled = getattr(self, fill_method)(limit=limit) fill_grp = filled.groupby(self.grouper.codes) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index e64dd99f26fb2..150651709047c 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2363,7 +2363,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0 ) ) if fill_method is None: # GH30463 - fill_method = 'pad' + fill_method = "pad" limit = 0 filled = getattr(self, fill_method)(limit=limit) fill_grp = filled.groupby(self.grouper.codes) From fb24fd64c3e09e41833d8318909f5b7070d8d7fd Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Tue, 31 Dec 2019 00:10:11 +0800 Subject: [PATCH 6/7] DOC: reformatted whatsnew entry (GH30463) --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 5a432f8f98250..4da0d0481fa2b 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -869,7 +869,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`) - Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`) - Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`) -- Bug in :meth:`GroupBY.pct_change` and `SeriesGroupBy.pct_change` causes TypeError when fill_method is None (GH30463) +- Bug in :meth:`GroupBy.pct_change` and :meth:`SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (GH30463) Reshaping ^^^^^^^^^ From a7c79c120410dcbc91dc185de285df3f8e6e30ef Mon Sep 17 00:00:00 2001 From: fujiaxiang Date: Tue, 31 Dec 2019 08:33:01 +0800 Subject: [PATCH 7/7] DOC: reformatted whatsnew entry (GH30463) --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 4da0d0481fa2b..fb636935f1480 100755 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -869,7 +869,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`) - Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`) - Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`) -- Bug in :meth:`GroupBy.pct_change` and :meth:`SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (GH30463) +- Bug in :meth:`GroupBy.pct_change` and :meth:`SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (:issue:`30463`) Reshaping ^^^^^^^^^