Skip to content

Commit eb637dc

Browse files
phoflmroeschke
authored andcommitted
BUG: pct_change showing unnecessary FutureWarning (pandas-dev#54983)
* BUG: pct_change showing unnecessary FutureWarning * Fix df case * Fix
1 parent f863469 commit eb637dc

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
@@ -11793,15 +11793,21 @@ def pct_change(
1179311793
stacklevel=find_stack_level(),
1179411794
)
1179511795
if fill_method is lib.no_default:
11796-
if self.isna().values.any():
11797-
warnings.warn(
11798-
"The default fill_method='pad' in "
11799-
f"{type(self).__name__}.pct_change is deprecated and will be "
11800-
"removed in a future version. Call ffill before calling "
11801-
"pct_change to retain current behavior and silence this warning.",
11802-
FutureWarning,
11803-
stacklevel=find_stack_level(),
11804-
)
11796+
cols = self.items() if self.ndim == 2 else [(None, self)]
11797+
for _, col in cols:
11798+
mask = col.isna().values
11799+
mask = mask[np.argmax(~mask) :]
11800+
if mask.any():
11801+
warnings.warn(
11802+
"The default fill_method='pad' in "
11803+
f"{type(self).__name__}.pct_change is deprecated and will be "
11804+
"removed in a future version. Call ffill before calling "
11805+
"pct_change to retain current behavior and silence this "
11806+
"warning.",
11807+
FutureWarning,
11808+
stacklevel=find_stack_level(),
11809+
)
11810+
break
1180511811
fill_method = "pad"
1180611812
if limit is lib.no_default:
1180711813
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)