Skip to content

Commit 19578e3

Browse files
fujiaxiangTomAugspurger
authored andcommitted
BUG: TypeError in groupby pct_change when fill_method is None (#30532)
* TST: added test for bug in groupby pct_change (GH30463)
1 parent d1f82f7 commit 19578e3

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ Groupby/resample/rolling
930930
- Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`)
931931
- Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`)
932932
- Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`)
933+
- Bug in :meth:`GroupBy.pct_change` and :meth:`SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (:issue:`30463`)
933934

934935
Reshaping
935936
^^^^^^^^^

pandas/core/groupby/generic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,9 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None):
809809
periods=periods, fill_method=fill_method, limit=limit, freq=freq
810810
)
811811
)
812+
if fill_method is None: # GH30463
813+
fill_method = "pad"
814+
limit = 0
812815
filled = getattr(self, fill_method)(limit=limit)
813816
fill_grp = filled.groupby(self.grouper.codes)
814817
shifted = fill_grp.shift(periods=periods, freq=freq)

pandas/core/groupby/groupby.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,9 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0
23622362
axis=axis,
23632363
)
23642364
)
2365+
if fill_method is None: # GH30463
2366+
fill_method = "pad"
2367+
limit = 0
23652368
filled = getattr(self, fill_method)(limit=limit)
23662369
fill_grp = filled.groupby(self.grouper.codes)
23672370
shifted = fill_grp.shift(periods=periods, freq=freq)

pandas/tests/groupby/test_transform.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -877,27 +877,19 @@ def test_pad_stable_sorting(fill_method):
877877
),
878878
],
879879
)
880-
@pytest.mark.parametrize(
881-
"periods,fill_method,limit",
882-
[
883-
(1, "ffill", None),
884-
(1, "ffill", 1),
885-
(1, "bfill", None),
886-
(1, "bfill", 1),
887-
(-1, "ffill", None),
888-
(-1, "ffill", 1),
889-
(-1, "bfill", None),
890-
(-1, "bfill", 1),
891-
],
892-
)
880+
@pytest.mark.parametrize("periods", [1, -1])
881+
@pytest.mark.parametrize("fill_method", ["ffill", "bfill", None])
882+
@pytest.mark.parametrize("limit", [None, 1])
893883
def test_pct_change(test_series, freq, periods, fill_method, limit):
894-
# GH 21200, 21621
884+
# GH 21200, 21621, 30463
895885
vals = [3, np.nan, np.nan, np.nan, 1, 2, 4, 10, np.nan, 4]
896886
keys = ["a", "b"]
897887
key_v = np.repeat(keys, len(vals))
898888
df = DataFrame({"key": key_v, "vals": vals * 2})
899889

900-
df_g = getattr(df.groupby("key"), fill_method)(limit=limit)
890+
df_g = df
891+
if fill_method is not None:
892+
df_g = getattr(df.groupby("key"), fill_method)(limit=limit)
901893
grp = df_g.groupby(df.key)
902894

903895
expected = grp["vals"].obj / grp["vals"].shift(periods) - 1

0 commit comments

Comments
 (0)