Skip to content

Commit 6db7b00

Browse files
Backport PR pandas-dev#54983 on branch 2.1.x (BUG: pct_change showing unnecessary FutureWarning) (pandas-dev#55040)
Backport PR pandas-dev#54983: BUG: pct_change showing unnecessary FutureWarning Co-authored-by: Patrick Hoefler <[email protected]>
1 parent a6445df commit 6db7b00

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

doc/source/whatsnew/v2.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Bug fixes
3434
~~~~~~~~~
3535
- Fixed bug for :class:`ArrowDtype` raising ``NotImplementedError`` for fixed-size list (:issue:`55000`)
3636
- Fixed bug in :meth:`DataFrame.stack` with ``future_stack=True`` and columns a non-:class:`MultiIndex` consisting of tuples (:issue:`54948`)
37+
- Fixed bug in :meth:`Series.pct_change` and :meth:`DataFrame.pct_change` showing unnecessary ``FutureWarning`` (:issue:`54981`)
3738

3839
.. ---------------------------------------------------------------------------
3940
.. _whatsnew_211.other:

pandas/core/generic.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -11692,15 +11692,21 @@ def pct_change(
1169211692
stacklevel=find_stack_level(),
1169311693
)
1169411694
if fill_method is lib.no_default:
11695-
if self.isna().values.any():
11696-
warnings.warn(
11697-
"The default fill_method='pad' in "
11698-
f"{type(self).__name__}.pct_change is deprecated and will be "
11699-
"removed in a future version. Call ffill before calling "
11700-
"pct_change to retain current behavior and silence this warning.",
11701-
FutureWarning,
11702-
stacklevel=find_stack_level(),
11703-
)
11695+
cols = self.items() if self.ndim == 2 else [(None, self)]
11696+
for _, col in cols:
11697+
mask = col.isna().values
11698+
mask = mask[np.argmax(~mask) :]
11699+
if mask.any():
11700+
warnings.warn(
11701+
"The default fill_method='pad' in "
11702+
f"{type(self).__name__}.pct_change is deprecated and will be "
11703+
"removed in a future version. Call ffill before calling "
11704+
"pct_change to retain current behavior and silence this "
11705+
"warning.",
11706+
FutureWarning,
11707+
stacklevel=find_stack_level(),
11708+
)
11709+
break
1170411710
fill_method = "pad"
1170511711
if limit is lib.no_default:
1170611712
limit = None

pandas/tests/frame/methods/test_pct_change.py

+18
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,21 @@ def test_pct_change_with_duplicated_indices(fill_method):
160160
index=["a", "b"] * 3,
161161
)
162162
tm.assert_frame_equal(result, expected)
163+
164+
165+
def test_pct_change_none_beginning_no_warning():
166+
# GH#54481
167+
df = DataFrame(
168+
[
169+
[1, None],
170+
[2, 1],
171+
[3, 2],
172+
[4, 3],
173+
[5, 4],
174+
]
175+
)
176+
result = df.pct_change()
177+
expected = DataFrame(
178+
{0: [np.nan, 1, 0.5, 1 / 3, 0.25], 1: [np.nan, np.nan, 1, 0.5, 1 / 3]}
179+
)
180+
tm.assert_frame_equal(result, expected)

pandas/tests/series/methods/test_pct_change.py

+8
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ def test_pct_change_with_duplicated_indices(fill_method):
107107

108108
expected = Series([np.nan, np.nan, 1.0, 0.5, 2.0, 1.0], index=["a", "b"] * 3)
109109
tm.assert_series_equal(result, expected)
110+
111+
112+
def test_pct_change_no_warning_na_beginning():
113+
# GH#54981
114+
ser = Series([None, None, 1, 2, 3])
115+
result = ser.pct_change()
116+
expected = Series([np.nan, np.nan, np.nan, 1, 0.5])
117+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)